iammilind
iammilind

Reputation: 70098

Is it valid to call a constexpr function in a template argument?

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
}

I have compiled this code with ideone. And was wondering that why it's giving compilation error. Is constexpr function not allowed as template argument or it's a bug in the compiler ?

Edit: changed const int get() to int get() Moreover, there is one more bug with ideone is that, if you remove constexpr then still declaring an array is allowed!! I think that's a C99 feature.

Upvotes: 11

Views: 1639

Answers (1)

Luc Danton
Luc Danton

Reputation: 35469

GCC 4.5 (at least the version used at Ideone) does not entirely support constexpr, including your valid usage; it waters down to a const. GCC 4.6 and up correctly supports it.

Upvotes: 13

Related Questions