在以下代码中
std::array<int,3> myarray = {10,20,30};
Run Code Online (Sandbox Code Playgroud)
我收到以下编译器警告
warning: missing braces around initializer for ‘std::array<int, 3u>::value_type [3] {aka int [3]}’ [-Wmissing-braces]
Run Code Online (Sandbox Code Playgroud)
为什么?
工具链:(编辑)
g++ (Ubuntu/Linaro 4.6.3-1ubuntu5) 4.6.3
Copyright (C) 2011 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
Run Code Online (Sandbox Code Playgroud)
正如Tyler所指出的,它std::array是一个POD,因此它没有构造函数,并且包含一个数组。要使用大括号语法进行初始化,请先初始化变量,然后使用嵌套的大括号初始化变量内部的数组。
{ { 10, 20, 30 } }
^ For the array member variable inside the std::array object
^ For the std::array object
Run Code Online (Sandbox Code Playgroud)
实际上,这是编译器中的一个错误,因为聚合初始化允许您删除=。之后的括号。所以这两个是合法的:
std::array<int,3> x = {10, 20, 30};
std::array<int,3> y {{10, 20, 30}};
Run Code Online (Sandbox Code Playgroud)
但不是
std::array<int,3> z {10, 20, 30};
Run Code Online (Sandbox Code Playgroud)
最后一个在GCC上编译,但这是非标准扩展,您应该得到警告。
| 归档时间: |
|
| 查看次数: |
935 次 |
| 最近记录: |