Why can#39;t simple initialize (with braces) 2D std::array?(为什么不能简单初始化(带大括号)2D std::array?)
问题描述
<块引用>可能的重复:
c++ 为什么 std::vector 的 initializer_list 行为和std::array 不同
我定义了简单的二维数组(3X2):
我很惊讶这个初始化不起作用,gcc4.5 错误:too many initializers for 'std::array
为什么我不能使用这种语法?
我找到了一些变通方法,一个非常有趣的额外括号,但只是想知道为什么第一种最简单的方法无效?
解决方法:
[更新]
好的,感谢 KerrekSB 和评论,我明白了.所以看起来我的例子中大括号太少了,就像这个C例子一样:
std::array
是一个集合,包含一个 C 数组.要初始化它,您需要为类本身使用外大括号,为 C 数组使用内大括号:
将此逻辑应用于二维数组给出:
Possible Duplicate:
c++ why initializer_list behavior for std::vector and std::array are different
I defined simple 2D array (3X2):
I was surprised this initialization does not work, with gcc4.5 error: too many initializers for 'std::array<std::array<int, 3u>, 2u>'
Why can't I use this syntax?
I found workarounds, one very funny with extra braces, but just wonder why the first, easiest approach is not valid?
Workarounds:
[UPDATE]
Ok, thanks to KerrekSB and comments I get the difference. So it seems that there is too little braces in my example, like in this C example:
std::array<T, N>
is an aggregate that contains a C array. To initialize it, you need outer braces for the class itself and inner braces for the C array:
Applying this logic to a 2D array gives this:
这篇关于为什么不能简单初始化(带大括号)2D std::array?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!