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

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

问题描述

好了,所以我试图通过初始化一堆在编译时constexpr静态常量INT 阵列做一些聪明。虽然运行时的性能完全不通过初始化这些数组管辖,这似乎是一个有趣的小运动。我写了一个测试设置,看看它是否是可能的,而且我最终能够做到这一点:

 结构测试
{
    constexpr静态常量INT数组[10] =阵列< INT,10,0,增量> ::阵列;
};constexpr诠释常量的Test ::阵列[10];诠释的main()
{
    COUT<<测试::阵列[3]<<的'\\ n';
}

在这里,阵列有一个名为阵列静态成员,其中包含10 INT S,从0开始,每个后续元素的值是通过一个名为模板的元编程函子来确定递增(即 { 0,1,...,9} )。正如预期的那样,该程序打印出号 3

真棒,对不对?我可以只写函子现在初始化数组在编译时各种时髦的图案。下一步:通过使非硬code数组大小10 测试 A类模板,像这样:

 模板<为size_t尺寸>
结构测试
{
    constexpr静态常量INT数组[尺寸] =阵列< INT,大小0,增量> ::阵列;
};模板<为size_t尺寸>
constexpr INT常量测试与LT;尺寸> ::阵列【尺寸】;诠释的main()
{
    COUT<<试验&所述10 -10; ::阵列[3]&下;&下;的'\\ n';
}

但是,突然现在不与消息编译:

  test.cc:43:72:错误:数组必须用括号括起来的初始化程序初始化

为什么会出现这种情况?是否有一个原因,一旦我把全班学生分成一类模板,或者我在GCC未实现的东西/车跌跌撞撞这种初始化已经失效了?

仅供参考,我可以发表我的code其余要求(阵列例如实施)。现在我认为这应该是足够了。

修改错误可以用不同的,琐碎的,实施阵列的复制来节省一些空间位置:

 模板<为size_t尺寸>
结构数组
{
    constexpr静态常量INT数组[尺寸] = {};
};模板<为size_t尺寸>
结构测试
{
    constexpr静态常量INT数组[尺寸] =阵列<尺寸和GT; ::阵列;
};


解决方案

以下是非法的;

 静态const int的一个[10] = {};
静态const int的B〔10] = A; //非法

所以GCC的错误,其实是对非模板的情况。

您可以使用的std ::阵列而不是C数组。

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

解决方案

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.

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

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