如果我尝试编译以下 C++0x 代码,则会收到错误消息:
template<int n> struct foo { };
struct bar {
static constexpr int number() { return 256; }
void function(foo<number()> &);
};
使用 gcc 4.6.1,错误消息是:
test.cc:6:27: error: ‘static constexpr int bar::number()’ used before its definition
test.cc:6:28: note: in template argument for type ‘int’
使用 clang 2.8,错误消息是:
test.cc:6:20: error: non-type template argument of type 'int' is not an integral
constant expression
void function(foo<number()> &);
^~~~~~~~
1 error generated.
如果我将constexpr
函数移动到基类,它可以在 gcc 上运行,并在 clang 上给出相同的错误消息:
template<int n> struct foo { };
struct base {
static constexpr int number() { return 256; }
};
struct bar : base {
void function(foo<number()> &);
};
代码是错误的,还是 gcc 4.6 的 C++0x 实现的限制或错误?如果代码错了,为什么错了,C++11标准的哪些条款说错了?