g++ (4.7.2) bug or feature, when initializing static arrays at compile-time?

不问归期 提交于 2019-12-07 07:23:55

问题


Okay, so I was trying to do something clever by initializing a bunch of constexpr static int const arrays at compile-time. Even though the runtime-performance is not at all governed by initializing these arrays, it seemed like a fun little exercise. I wrote a test-setup to see if it was possible, and I ended up being 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, Array has a static member called array which contains 10 ints, starting at 0, where the value of each subsequent element is determined by a Template-Metaprogramming functor called Increment (i.e. {0, 1, ..., 9}). As expected, the program prints out the number 3.

Awesome, right? I can just write functors now and initialize arrays will all kinds of funky patterns at compile-time. Next step: un-hardcode the array-size 10 by making Test a class-template like so:

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';
}

However, all of a sudden it doesn't compile anymore with the message:

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

Why does this happen? Is there a reason that this kind of initialization has become invalid once I turn the class into a class-template, or have I stumbled upon something unimplemented/buggy in GCC?

FYI, I can post the rest of my code (the implementation of Array for example) on request. For now I think this should be enough.

EDIT The error can be reproduced with a different, trivial, implementation of Array to save some space here:

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;
};

回答1:


Following is illegal;

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

So the bug of gcc is in fact for the non template case.

You may use std::array instead of C-array.



来源:https://stackoverflow.com/questions/21435431/g-4-7-2-bug-or-feature-when-initializing-static-arrays-at-compile-time

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!