Array initialization using constexpr?

I wonder if it is possible to initialize the entire array using the constexpr function (since C ++ 2011). Here I have something to illustrate what I want to do:

template<unsigned int DIM> const unsigned int MyClass<DIM>::_myVar[2][3] = { {metaFunction(0, 0, DIM), metaFunction(0, 1, DIM), metaFunction(0, 2, DIM)}, {metaFunction(1, 0, DIM), metaFunction(1, 1, DIM), metaFunction(1, 2, DIM)} }; template<unsigned int DIM> inline constexpr unsigned int MyClass<DIM>::metaFunction(const unsigned int k, const unsigned int n, const unsigned int dim) { return (((n < dim) && (k < n)) ? (1<<(nk)) : (0)); } 

Is there a way to initialize myVar using constexpr without manually populating the array. And if it exists, what will be the syntax for this example?

To ask the exact question, I'm looking for a way to populate all myVar values ​​with a single function call.

+7
source share
1 answer

Without seeing the definition of MyClass , the problem is not entirely clear. In any case, I believe that you want to get MyClass::_myVar without iteratively MyClass::metaFunction() it with the values ​​of MyClass::metaFunction() .

You indicate that MyClass::_myVar is a member of a static class. In this case, when your element initialization is great for C ++ 11. The following program illustrates (GCC 4.6.3):

 #include <iostream> /* MyClass Version 1 */ template<unsigned int DIM> struct MyClass { static constexpr unsigned int metaFunction( const unsigned int k, const unsigned int n, const unsigned int dim); static const unsigned int _myVar[2][3]; }; template<unsigned int DIM> inline constexpr unsigned int MyClass<DIM>::metaFunction( const unsigned int k, const unsigned int n, const unsigned int dim) { return (((n < dim) && (k < n)) ? (1<<(nk)) : (0)); } template<unsigned int DIM> const unsigned int MyClass<DIM>::_myVar[2][3] = { { metaFunction(0, 0, DIM), metaFunction(0, 1, DIM), metaFunction(0, 2, DIM) }, { metaFunction(1, 0, DIM), metaFunction(1, 1, DIM), metaFunction(1, 2, DIM) } }; template<unsigned int DIM> inline constexpr unsigned int MyClass<DIM>::metaFunction( const unsigned int k, const unsigned int n, const unsigned int dim) { return (((n < dim) && (k < n)) ? (1<<(nk)) : (0)); } using namespace std; int main(void) { MyClass<3> mine; for (int i = 0; i < 2; ++i) { for (int j = 0; j < 3; ++j) { cout << mine._myVar[i][j] << endl; } } return 0; } 

This makes me think that MyClass::_myVar not a static member - although why this array of integer constants would not be static, I'm not sure. If so, you can initialize the default member constructor using uniform C ++ 11 initialization :

 /* MyClass Version 2 */ template<unsigned int DIM> struct MyClass { MyClass() : _myVar{ { MyClass::metaFunction(0, 0, DIM), MyClass::metaFunction(0, 1, DIM), MyClass::metaFunction(0, 2, DIM) }, { MyClass::metaFunction(1, 0, DIM), MyClass::metaFunction(1, 1, DIM), MyClass::metaFunction(1, 2, DIM) } }{} static constexpr unsigned int metaFunction( const unsigned int k, const unsigned int n, const unsigned int dim); const unsigned int _myVar[2][3]; }; 

In any case, the constexpr metaFunction attribute is actually necessary for compilation. And if constexpr is removed then /* MyClass Version 1*/ also good for C ++ 03.

+3
source

All Articles