使用constexpr函数作为模板参数是否有效?

iam*_*ind 10 c++ templates compiler-errors constexpr c++11

constexpr int get () { return 5; }
template<int N> struct Test {};

int main ()
{
  int a[get()];  // ok
  Test< get() > obj;  // error:'int get()' cannot appear in a constant-expression
}
Run Code Online (Sandbox Code Playgroud)

用ideone编译了这段代码.并且想知道为什么它会给出编译错误.是constexpr不允许函数作为template参数,还是编译器中的错误?

编辑:更改const int get()int get() 此外,还有一个与ideone的错误是,如果你删除constexpr然后仍然声明一个数组是允许的!我认为这是一个C99功能.

Luc*_*ton 13

GCC 4.5(至少在Ideone使用的版本)并不完全支持constexpr,包括您的有效使用; 水流到水面const.GCC 4.6及以上版本正确支持它.