64位G ++ 4.6.3不将longs作为long longs在特殊功能模板中,即使它们是相同的大小。这是一个错误? [英] 64-bit G++ 4.6.3 doesn't treat longs as long longs in specialised function templates, even though they're the same size. Is this a bug?
问题描述
请考虑以下代码:
#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位G ++ 4.6.3成功编译并生成输出: / p>
32-bit G++ 4.6.3 compiles this successfully and produces the output:
sizeof(long)=4
sizeof(long long)=8
f<long long>()
在64位G ++ 4.6.3下编译会产生链接器错误:
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
有64位G ++处理 f< long>
code> long 和 long long $ c>作为不同的函数,code> f< long long> $ c>是相同的大小,还是这是我应该报告的错误?
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?
推荐答案
int64_t
可以是任何符合要求的东西。可以在一个平台上使长
,在另一个平台上使长长
。
The underlying type of int64_t
can be anything that meets the requirements. It's okay to make it long
on one platform and long long
on another.
因为你为 long long
提供了一个特殊化,而泛型版本没有正文,如果 int64_t
不是 long long
,你得到一个未定义的引用。
Since you provide a specialization for long long
and the generic version has no body, if int64_t
is not a long long
you get an undefined reference.
是的,有一个很好的理由为什么 f< long>
和 f< long long>
是不同的功能: long 和 long long
是不同类型。事实上,它们在一些平台上的宽度相同并不重要,特别是因为它们在另一个平台上的宽度可能不同。
And yes, there is a good reason why f<long>
and f<long long>
are different functions: it's because the standard says that long
and long 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.
这篇关于64位G ++ 4.6.3不将longs作为long longs在特殊功能模板中,即使它们是相同的大小。这是一个错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!