首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

关于typeid().name()的返回值有关问题

2012-02-22 
关于typeid().name()的返回值问题#includeiostream#includestring#includetypeinfousing namespace

关于typeid().name()的返回值问题
#include<iostream>
#include<string>
#include<typeinfo>
using namespace std;

template<typename T>
struct Type{
  static void _print_e_a(){
  cout<<"sizeof("<<typeid(T).name()<<")="<<sizeof(T)<<endl;
  }
  };

int main()
{  
  Type<bool>::_print_e_a();
  Type<char>::_print_e_a();
  Type<signed char>::_print_e_a();
  Type<wchar_t>::_print_e_a();
  Type<signed short>::_print_e_a();
  Type<unsigned short >::_print_e_a();
  Type<signed int >::_print_e_a();
  Type<unsigned int >::_print_e_a();
  Type<signed long >::_print_e_a();
  Type<unsigned long >::_print_e_a();
}

上面程序的运行结果是这样的,通道typeid().name()不是返回T的类型名称啊,怎么是a,b,c...呢???
我用的是Dev C++ 4.9.9.2
sizeof(b)=1
sizeof(c)=1
sizeof(a)=1
sizeof(w)=2
sizeof(s)=2
sizeof(t)=2
sizeof(i)=4
sizeof(j)=4
sizeof(l)=4
sizeof(m)=4



[解决办法]
这个,标准没有规定的
各个编译器结果都不一样
在我的INTEL编译器下:
sizeof(bool)=1
sizeof(char)=1
sizeof(signed char)=1
sizeof(unsigned short)=2
sizeof(short)=2
sizeof(unsigned short)=2
sizeof(int)=4
sizeof(unsigned int)=4
sizeof(long)=4
sizeof(unsigned long)=4

热点排行