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.


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.

链接地址: http://www.djcxy.com/p/10750.html

上一篇: 如何检查x86汇编中的密钥状态?

下一篇: 用constexpr初始化一个数组?