std::any测试
#include <any>
class A {
public:
int8_t a;
};
int main(int argc, char* argv[]) {
std::any num((int8_t)42);
auto a = std::any_cast<A>(num);
return 0;
}
异常: 0x00007FFA9385CD29 处(位于 test.exe 中)有未经处理的异常: Microsoft C++ 异常: std::bad_any_cast,位于内存位置 0x000000882B96FC50 处。
SafeAny测试
#include "SafeAny/safe_any.hpp"
class A {
public:
int8_t a;
};
int main(int argc, char* argv[]) {
Any num( (int8_t) 42 );
auto a = num.cast<A>();
}
异常:D:\SafeCppAny\test.cpp:12: Failure: due to unexpected exception with message: [Any::convert]: no known safe conversion between __int64 and class A
优点
SafeAny比std::any增加了错误位置,错误类型转换说明,方便问题定位
参考
GitHub - facontidavide/SafeCppAny: Extension of std::any with small object optmization and safe numeric conversion.