以下代码可在Intel-2015上正常编译,但在gcc 4.8.3下无法编译谁是对的?
#include <iostream>
void f( int const& x ) { std::cout << x << std::endl; }
struct S
{
static constexpr int ce = 42;
};
int main()
{
f(S::ce);
return 0;
}
g ++错误:
/tmp/ccOIxa2V.o: In function `main':
test_constexpr.cpp:(.text+0x36): undefined reference to `S::ce'
collect2: error: ld returned 1 exit status
由于该函数f
采用引用参数,因此必须定义S::ce
引用可以在运行时指向的参数;编译器不能只用文字42代替参数。因此,您必须添加类外定义:
const int S::ce;
就像使用非constexpr
变量一样。这将在运行时为该值分配一个内存位置,以供引用和编译时无法计算的其他内容使用。
有关更多信息,请参见此GCC错误报告(其中有注释解释了为什么它实际上不是错误)。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句