std::type_info:: name
|
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Basic types | |||||||||||||||||||||
| Fixed width integer types (C++11) | |||||||||||||||||||||
| Fixed width floating-point types (C++23) | |||||||||||||||||||||
|
|||||||||||||||||||||
| Numeric limits | |||||||||||||||||||||
| C numeric limits interface | |||||||||||||||||||||
| Runtime type information | |||||||||||||||||||||
|
|||||||||||||||||||||
| Member functions | ||||
|
(until C++20)
|
||||
|
(C++11)
|
||||
|
type_info::name
|
|
const
char
*
name
(
)
const
;
|
(自 C++11 起为 noexcept) | |
返回一个由实现定义的、以空字符结尾的字符串,其中包含该类型的名称。不提供任何保证;特别是,对于多个类型返回的字符串可能相同,并且同一程序的不同调用之间可能发生变化。
目录 |
参数
(无)
返回值
以空字符结尾的字符串 包含该类型的名称。
注释
返回指针所指向数组的生命周期未作明确规定,但在实践中,只要给定类型的RTTI数据结构存在,该数组就会持续存在——这些数据结构具有应用程序级生命周期,除非是从可卸载的动态库中加载的。
某些实现(如MSVC、IBM、Oracle)会生成人类可读的类型名称。而其他实现(最著名的是gcc和clang)会返回经过名称修饰的符号,该格式由
Itanium C++ ABI
规范定义。可通过实现特定的API(例如直接使用
abi::__cxa_demangle
或通过
boost::core::demangle
)将修饰后的名称转换为人可读形式。也可通过命令行工具
c
++
filt
-
t
进行管道处理。
示例
#include <boost/core/demangle.hpp> #include <cstdlib> #include <iostream> #include <string> #include <typeinfo> struct Base { virtual ~Base() = default; }; struct Derived : Base {}; int main() { Base b1; Derived d1; const Base* pb = &b1; std::cout << typeid(*pb).name() << '\n'; pb = &d1; std::cout << typeid(*pb).name() << '\n'; std::string real_name = boost::core::demangle(typeid(pb).name()); std::cout << typeid(pb).name() << " => " << real_name << '\n'; std::cout << "c++filt => " << std::flush; std::string s = typeid(pb).name(); std::system(("c++filt -t " + s).data()); }
可能的输出:
// GCC/Clang: 4Base 7Derived PK4Base => Base const* c++filt => Base const* // MSVC: struct Base struct Derived struct Base const * __ptr64 => struct Base const * __ptr64
参见
|
(C++11)
|
返回相同类型对应的相同值
(公开成员函数) |