我在使用C++类型特征时遇到了一些奇怪的行为,并将我的问题缩小到这个古怪的小问题,我将给出大量的解释,因为我不想留下任何误解的东西.
假设您有这样的程序:
#include <iostream>
#include <cstdint>
template <typename T>
bool is_int64() { return false; }
template <>
bool is_int64<int64_t>() { return true; }
int main()
{
std::cout << "int:\t" << is_int64<int>() << std::endl;
std::cout << "int64_t:\t" << is_int64<int64_t>() << std::endl;
std::cout << "long int:\t" << is_int64<long int>() << std::endl;
std::cout << "long long int:\t" << is_int64<long long int>() << std::endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
在使用GCC(以及32位和64位MSVC)进行32位编译时,程序的输出将为:
int: 0
int64_t: 1
long int: 0
long long int: 1
Run Code Online (Sandbox Code Playgroud)
但是,由64位GCC编译产生的程序将输出:
int: 0
int64_t: 1 …Run Code Online (Sandbox Code Playgroud)