异常的不良TypeInfo名称
Bad typeinfo name for exceptions
当我运行以下程序时,我会得到一个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) : 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 caughtn";
}
}
这是输出:
捕获的例外:st19really_out_of_range:x> 20
但是,当我将test()
函数上的NOExcept规范更改为true
以触发std::terminate()
的调用时,我会得到此输出:
libc abi.dylib:终止以未被发现的类型 std :: trume_out_of_range:x> 20
我认为std :: terminate((可能能够提供命名的无管状类型,因为它明确处理了每个标准异常类型,但这显然不是这种情况,因为它也正确处理了我上面定义的新异常类型。
所以,我的问题是为什么typeInfo名称在std::terminate()
中是正确的,而当我尝试直接访问它时?或者,更重要的是,std::terminate()
调用提供了什么函数?
cdhowie的评论后,我在这里找到答案:拆开std :: type_info的结果:: name
基本上,您使用#include <cxxabi.h>
并致电abi::__cxa_demangle
,如果您使用的是GNU。但是您必须小心内存管理。
但是,您也可以使用Boost。然后,您只需 #include <boost/core/demangle.hpp>
并致电boost::core::demangle( name )
。
。
相关文章:
- 为什么两个不同的未命名名称空间可以共存于一个cpp文件中
- 当在同一名称空间中有两个具有相同签名的函数时,会发生什么
- 预处理器:插入结构名称中的前一个行号
- 基类中的函数名称解析
- 模板元程序查找相似的连续类型名称
- 没有名称的C++模板参数
- C++中名称篡改的目的是什么
- 如何仅使用对象名称打印特定于对象的成员
- 将依赖名称显式标记为类型名和模板的奇怪之处
- 为什么 Clang 不允许"and"作为函数名称?
- 遮罩结构名称
- c++binary_search函数排序数组(流行名称搜索)出现问题
- RegGetValue在当前用户下找不到名称
- 如何打开并写入一个名称取自C++中字符串的文件
- 在没有参数列表的情况下使用模板名称"Event"无效,模板问题
- 公开最直接的基类模板名称
- Bjarne Stroustrup Book - std_lib_facilities.h - 不起作用(未知类型名称)
- 为什么用结构初始化数组需要指定结构名称
- 使用单词"not"作为C ++类的名称会导致VS2019错误
- 异常的不良TypeInfo名称