Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Initializing an array with a constexpr?

I wonder if it is possible to initialize an entire array with a constexpr function (with 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<<(n-k)) : (0));
}

Is there a way to initialize myVar with a constexpr without filling the array manually. And if it exists, what would be the syntax for the given example ?

To precise the question a little, I search for a way to fill all values of myVar using a single function call.

like image 448
Vincent Avatar asked May 10 '12 06:05

Vincent


1 Answers

Without seeing the definition of MyClass the problem is not quite clear. I believe anyway that you want to get MyClass::_myVar initialized without code to iteratively fill it with the MyClass::metaFunction() values.

You code suggests that MyClass::_myVar is a static class member. In that case your initialization of the member is perfectly good C++11 as it stands. 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<<(n-k)) : (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<<(n-k)) : (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 inclines me to think that MyClass::_myVar is not a static member - although why this array of integer constants would not be static I am not sure. If that is the case, then you can initialize the member in the default constructor using the uniform initialization provision of C++11:

/* 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 neither case is the constexpr attribute of metaFunction actually necessary for compilation. And if constexpr is removed then /* MyClass Version 1*/ is also good for C++03.

like image 68
Mike Kinghan Avatar answered Sep 23 '22 22:09

Mike Kinghan