C++ 异常的typeinfo名称不正确
当我运行下面的程序时,我得到了一个损坏的typeinfo名称C++ 异常的typeinfo名称不正确,c++,exception-handling,typeid,typeinfo,C++,Exception Handling,Typeid,Typeinfo,当我运行下面的程序时,我得到了一个损坏的typeinfo名称 #include <iostream> #include <stdexcept> #include <typeinfo> namespace std { class really_out_of_range : public out_of_range { public: explicit really_out_of_range(const string& __s) : ou
#include <iostream>
#include <stdexcept>
#include <typeinfo>
namespace std
{
class really_out_of_range
: public out_of_range
{
public:
explicit really_out_of_range(const string& __s) : out_of_range(__s) {}
explicit really_out_of_range(const char* __s) : out_of_range(__s) {}
virtual ~really_out_of_range() _NOEXCEPT {}
};
}
void test () noexcept(false)
{
throw std::really_out_of_range("x > 20");
}
int main () noexcept(true)
{
try {
test();
} catch (const std::exception& e) {
std::cout << "Exception caught: " << typeid(e).name() << ": " << e.what() << '\n';
} catch (...) {
std::cout << "Unknown exception caught\n";
}
}
#包括
#包括
#包括
名称空间标准
{
类确实超出了范围
:公共超出范围
{
公众:
显式真正超出范围(const string&\uu s):超出范围(\uu s){
显式真超出超出超出超出超出超出超出超出超出超出超出超出超出超出范围({}
virtual~really_out_of_range()_NOEXCEPT{}
};
}
无效测试()无异常(错误)
{
抛出标准::真的超出范围(“x>20”);
}
int main()无异常(true)
{
试一试{
test();
}捕获(const std::exception&e){
std::cout在他的评论之后,我在这里找到了答案:
基本上,如果您使用GNU,您可以#include
并调用abi::u cxa\u demangle
。但是您必须小心内存管理
但是,您也可以使用boost。然后您只需#包括并调用boost::core::demangle(name)
.我怀疑std::terminate
只是要求输入typeinfo名称。它可能使用了一个环境内部函数。例如,G++具有abi::\uuucxa\udemangle
。typeid(任何内容).name()的结果是实现定义的。std::terminate()
不需要demangle(或以任何特定形式表示异常类型),也没有标准函数来demangle名称。最后,向命名空间std
添加声明会产生未定义的行为。