Strange output of std::typeid::name()
我使用typeid来获取std :: vector :: size_type的类型名称和大小为零的类A,其代码如下(cppreference):
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| #include<iostream>
#include <vector>
#include <typeinfo>
using namespace std;
class A {};
int main()
{
vector<int> v(10);
vector<int>::size_type s = v.size();
A a;
cout << typeid(s).name() << endl;
cout << typeid(a).name() << endl;
}; |
我得到这个作为输出:
我想" A"之前的" 1"是空基类优化的结果,但是" m"代表什么,这是正常现象吗?
我正在使用以下gcc版本:g ++(Ubuntu 4.4.3-4ubuntu5.1)4.4.3
-
type_info::name返回的名称是实现定义的。 Gcc为size_type选择了m。 甘拜下风 :)
-
@jrok啊,那好吧。 :)
-
您可能要检查std::type_info::name函数上的引用。
-
@JoachimPileborg我做到了,但是我想我期待的不仅仅是一个" m"字符。 :)
-
" A"之前的" 1"是因为这就是名称A的长度。
-
可能重复的人类可读type_info.name()
G ++对类型使用实现定义的命名,但它也提供实用程序c++filt使其易于阅读:
1 2 3
| $ ./test | c++filt -t
unsigned long
A |
-
是! 真好! 谢谢! :)
-
那么,名称" m"然后被保留并固定用于vector :: size_type(对于g ++)?
-
@ tomislav-maric,名称" m"代表unsigned long,恰好是std::size_t的typedef,而恰好是std::vector<>::size_type的typedef。 Typedef不会创建新类型。
-
我知道他们不会创建新类型,我的意思是:是" m",然后对于G ++,它的名称是" unsigned_long",并且在某个地方定义(编译器实现?),因此c ++ filt会在其中查找并映射它 回到" unsigned_long"?
-
@ tomislav-maric是的,usrbing++和usrbinc++filt是同一编译器实现的一部分。