Consider the following code:
#include <iostream>
#include <cinttypes>
template<class T>
void f();
template<>
inline void f<long long>() {
std::cout<<"f<long long>()"<<std::endl;
}
int main(int , char** ) {
std::cout<<"sizeof(long)="<<sizeof(long)<<std::endl;
std::cout<<"sizeof(long long)="<<sizeof(long long)<<std::endl;
f<int64_t>();
return 0;
}
32-bit G++ 4.6.3 compiles this successfully and produces the output:
sizeof(long)=4
sizeof(long long)=8
f<long long>()
Compiling under 64-bit G++ 4.6.3 however produces the linker error:
undefined reference to `void f<long>()'
ld returned 1 exit status
even though compiling and running with the f<int64_t>() line commented out produces:
sizeof(long)=8
sizeof(long long)=8
Is there a good reason why 64-bit G++ treats f<long> and f<long long> as different functions, even though long and long long are the same size, or is this a bug that I should report?
The underlying type of
int64_tcan be anything that meets the requirements. It’s okay to make itlongon one platform andlong longon another.Since you provide a specialization for
long longand the generic version has no body, ifint64_tis not along longyou get an undefined reference.And yes, there is a good reason why
f<long>andf<long long>are different functions: it’s because the standard says thatlongandlong longare distinct types. The fact that they happen to be the same width on some platform doesn’t matter, especially because they may be of different widths on another.