在编译时初始化静态数组时,g ++(4.7.2)错误或特性? [英] g++ (4.7.2) bug or feature, when initializing static arrays at compile-time?

查看:146
本文介绍了在编译时初始化静态数组时,g ++(4.7.2)错误或特性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好吧,所以我试图通过在编译时初始化一堆 constexpr static int const 数组来做一些聪明。即使运行时性能根本不受初始化这些数组的控制,它似乎是一个有趣的小练习。我写了一个测试设置,看看是否可能,我最终能够这样做:

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

这里, Array 有一个名为 array 的静态成员,其中包含10 int s,从0开始,其中每个后续元素的值为由增量(即 {0,1,...,9} )的模板元编程函子确定。如预期,程序打印出 3

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.

真棒,对不对?我现在可以只写写函数,并在编译时初始化数组将各种有趣的模式。下一步:通过使 Test 类模板来对数组大小为10进行硬编码:

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

为什么会发生这种情况?有一个原因,这种初始化已经变得无效,一旦我把类转换成类模板,或者我偶然发现了一些未实现/在GCC中的bug。

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,我可以根据请求发布我的代码的剩余部分(例如 Array 的实现)。现在我认为这应该就够了。

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.

编辑错误可以通过 Array ,以在此处节省一些空间:

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


推荐答案

以下是非法的:

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

所以gcc的错误实际上是非模板case。

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

您可以使用 std :: array 而不是C-array。

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

这篇关于在编译时初始化静态数组时,g ++(4.7.2)错误或特性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆