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?
They are different types, so they have to be treated differently. The size of the types does not affect overloading or template selection.
Your problem might be that
int64_t
islong
on one system andlong long
on the other. That's a problem when combining typedefs with overloads or templates.The underlying type of
int64_t
can be anything that meets the requirements. It's okay to make itlong
on one platform andlong long
on another.Since you provide a specialization for
long long
and the generic version has no body, ifint64_t
is not along long
you 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 thatlong
andlong long
are 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.