在 Microsoft VC2005 和 g++ 编译器中,以下导致错误:
在 win32 VC2005 上:sizeof(wchar_t) 为 2
wchar_t *foo = 0;
static_cast<unsigned short *>(foo);
结果是
error C2440: 'static_cast' : cannot convert from 'wchar_t *' to 'unsigned short *' ...
在 Mac OS X 或 Linux g++ 上:sizeof(wchar_t) 为 4
wchar_t *foo = 0;
static_cast<unsigned int *>(foo);
结果是
error: invalid static_cast from type 'wchar_t*' to type 'unsigned int*'
当然,我总是可以使用reinterpret_cast。但是,我想了解为什么编译器认为将 static_cast 转换为适当的整数类型是非法的。我敢肯定有一个很好的理由...