G ++ (4.7.2) error or function when initializing static arrays at compile time?

Ok, so I tried to do something smart by initializing a bunch of arrays constexpr static int constat compile time. Although runtime performance is not regulated by initializing these arrays, this seemed like a fun little exercise. I wrote a test setup to make sure this was possible, and I was able to do this:

struct Test
{
    constexpr static int const array[10] = Array<int, 10, 0, Increment>::array;
};

constexpr int const Test::array[10];

int main()
{
    cout << Test::array[3] << '\n';
}

Here Arrayhas a static member called Arraythat contains 10 ints, starting at 0, where the value of each subsequent element is determined using the Template-Metaprogramming functor called Increment(i.e. {0, 1, ..., 9}). As expected, the program will display the number 3.

, ? , . : un-hardcode 10, Test - :

template <size_t Size>
struct Test
{
    constexpr static int const array[Size] = Array<int, Size, 0, Increment>::array;
};

template <size_t Size>
constexpr int const Test<Size>::array[Size];

int main()
{
    cout << Test<10>::array[3] << '\n';
}

:

test.cc:43:72: error: array must be initialized with a brace-enclosed initializer

? , , , - / GCC?

FYI, (, Array) . , .

EDIT , Array :

template <size_t Size>
struct Array
{
    constexpr static int const array[Size] = {};
};

template <size_t Size>
struct Test
{
    constexpr static int const array[Size] = Array<Size>::array;
};
+4
1

;

static const int a[10] = {};
static const int b[10] = a; // Illegal

, gcc .

C- std::array.

+4

Source: https://habr.com/ru/post/1524308/


All Articles