Constexpr, templates and array size

c++, c++11, templates

Solution

In C++11, a `constexpr` function can only contain a `return` statement (see here for the full details), so your `myPow` function is not constexpr-compliant (because it contains a `for` loop).

You can use this metafunction to compute the integer power at compile-time:

template <int N, typename Type> 
constexpr Type pow(const Type& x) 
{
    return (N > 1) ? (x*pow<(N-1)*(N > 1)>(x)) 
                   : ((N < 0) ? (static_cast<Type>(1)/pow<(-N)*(N < 0)>(x)) 
                              : ((N == 1) ? (x) 
                                          : (static_cast<Type>(1))));
}

If you want to compute `2^N`, you can type:

pow<N>(2)

Note 1: this metafunction is very generic and also work for negative integers and floating-point types, so you can type : `pow<-3>(3.14)`

Note 2: the multiplication by `N>1` or `N<0` in the template are here to block infinite recursivity and force the template parameter to be equal to zero when the branch is not relevant. This could be done with template specialization, but the technique used here allow to write a single function.

Problem

I would like to pass template argument to function call and the return value use as size of an array ie ``` constexpr int myPow(int a, int b){ int result = 1; for(int i=0;i<b;i++) result *= a; return result; } template <int N> class testClass{ public: testClass(){} int array[myPow(2,N)]; }; int main(){ testClass<3> A; return 0; } ``` compiler errors: ``` ~ $ g++-4.6 test.cpp -std=gnu++0x test.cpp: In function ‘constexpr int myPow(int, int)’: test.cpp:6:1: error: body of constexpr function ‘constexpr int myPow(int, int)’ not a return-statement test.cpp: At global scope: test.cpp:12:23: error: array bound is not an integer constant before ‘]’ token ``` Any idea how to get around this?

Original source