是否可能将数据作为initializer_list传递给结构的std :: array? [英] Is it possible to pass data as initializer_list to std::array of structures?

查看:558
本文介绍了是否可能将数据作为initializer_list传递给结构的std :: array?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码。基本上,我想使用聚合初始化语法初始化非POD结构的std ::数组。 g ++ 4.6和4.7(最新的每周快照)无法编译代码。

  #include< array& 

struct TheClass {

int a1,a2;

TheClass(int b1,int b2):a1 {b1},a2 {b2} {};
};

template< unsigned D>
struct OtherClass {

std :: array< TheClass,D>一个;

OtherClass(s​​td :: array< TheClass,2> b):a {b} {};
};

int main()
{
OtherClass 2 {{1,2},{2,3}}}; //尝试了很多选项
}

GCC错误:

  v.cpp:在函数'int main()':
v.cpp:20:37:error:调用'OtherClass< 2u> :: OtherClass(<括号括起始化程序列表>)'
v.cpp:20:37:注意:候选项是:
v.cpp:15:注意:对于参数1没有已知的转换,因为参数1从''' <括号括起始化列表>'到'std :: array< TheClass,2ul>'
v.cpp:11:8:note:constexpr OtherClass< 2u> :: OtherClass(const OtherClass< 2u& ;)
v.cpp:11:8:note:没有已知的从'<包含初始化器列表>'到'const OtherClass< 2u>&'
v.cpp :11:8:note:constexpr OtherClass< 2u> :: OtherClass(OtherClass< 2u&&&)
v.cpp:11:8:注意: -enclosed initializer list>'to'OtherClass< 2u>&'

上面的代码是否正确?看来,作为std :: array是一个聚合,应该没有问题来构造其数据成员。也许这是GCC的错误?



EDIT



OtherClass< 2> {{TheClass {1,2},TheClass {2,3}}}; 当然,但是我不想使用,因为我必须构造类在很多地方。 C ++ 11应该支持省略 TheClass 。另请参阅这个问题

解决方案

std :: array 是一个包含数组的聚合 ,因此您需要一对额外的大括号来从常规数组初始化:

  {1,2} // TheClass 
{{1,2},{2,3}} // TheClass [2]
{{1,2 },{2,3}}} // std :: array< TheClass,2>
{{{{1,2},{2,3}}}} // OtherClass< 2>然而,似乎较旧版本的GCC仍然困扰着这个: //ideone.com/aWTVorel =nofollow> 4.5.1拒绝,而4.6.1接受它。


I have the following code. Basically I want to initialize a std::array of non-POD structs using aggregate initialization syntax. Both g++ 4.6 and 4.7 (latest weekly snapshot) fails to compile the code.

#include <array>

struct TheClass {

    int a1, a2;

    TheClass(int b1, int b2) : a1{b1}, a2{b2} {};    
};

template<unsigned D>
struct OtherClass {

    std::array<TheClass, D> a;

    OtherClass(std::array<TheClass, 2> b) : a{b} {};
};

int main()
{
    OtherClass<2>{{ {1, 2}, {2, 3} }}; //tried a lot of options here
}

GCC errors:

v.cpp: In function ‘int main()’:
v.cpp:20:37: error: no matching function for call to ‘OtherClass<2u>::OtherClass(<brace-enclosed initializer list>)’
v.cpp:20:37: note: candidates are:
v.cpp:15:5: note: OtherClass<D>::OtherClass(std::array<TheClass, 2ul>) [with unsigned int D = 2u]
v.cpp:15:5: note:   no known conversion for argument 1 from ‘<brace-enclosed initializer list>’ to ‘std::array<TheClass, 2ul>’
v.cpp:11:8: note: constexpr OtherClass<2u>::OtherClass(const OtherClass<2u>&)
v.cpp:11:8: note:   no known conversion for argument 1 from ‘<brace-enclosed initializer list>’ to ‘const OtherClass<2u>&’
v.cpp:11:8: note: constexpr OtherClass<2u>::OtherClass(OtherClass<2u>&&)
v.cpp:11:8: note:   no known conversion for argument 1 from ‘<brace-enclosed initializer list>’ to ‘OtherClass<2u>&&’

My questions: is the above code correct? It seems that as std::array is an aggregate, there should be no problems to construct its data members. Maybe it's a bug in GCC?

EDIT:

OtherClass<2>{{ TheClass{1, 2}, TheClass{2, 3} }}; works of course, but I don't want to use that as I have to construct the class in a lot of places. C++11 should support omission of TheClass. Also see this question.

解决方案

std::array is an aggregate containing an array, so you need an extra pair of braces to initialise it from a regular array:

{1,2}               // TheClass
{{1,2},{2,3}}       // TheClass[2]
{{{1,2},{2,3}}}     // std::array<TheClass,2>
{{{{1,2},{2,3}}}}   // OtherClass<2>

However, it seems that older versions of GCC still stuggle with this: 4.5.1 rejects it, while 4.6.1 accepts it.

这篇关于是否可能将数据作为initializer_list传递给结构的std :: array?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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