比较代码:
const char x = 'a';
std::cout<< x;
00C31000 mov eax,dword ptr [__imp_std::cout (0C32054h)]
00C31005 push eax
00C31006 call std::operator<<<std::char_traits<char> > (0C310B0h)
00C3100B add esp,4
和
const int x = 'a';
std::cout<< x;
00271000 mov ecx,dword ptr [__imp_std::cout (272048h)]
00271006 push 61h
00271008 call dword ptr [__imp_std::basic_ostream<char,std::char_traits<char> >::operator<< (272044h)]
和
const char* x = "a";
std::cout<< x;
00071000 mov eax,dword ptr [__imp_std::cout (72058h)]
00071005 push eax
00071006 call std::operator<<<std::char_traits<char> > (710B0h)
0007100B add esp,4
似乎该 问题 - 为什么生成的代码有差异?const int
版本比const char*
和(更令人惊讶的)const char
版本优化得更好。