为什么我需要一个类似于复合文字的临时构造来初始化我的std::数组成员
Why do I need a compound literal like temporary construction to initialise my std::array member?
考虑这个最小的例子:
#include <array>
struct X {
std::array<int,2> a;
X(int i, int j) : a(std::array<int,2>{{i,j}}) {}
// ^^^^^^^^^^^^^^^^^^ ^
};
根据其他帖子,在这种情况下,我不应该明确地构建一个临时的。我应该能够写:
X(int i, int j) : a{{i,j}} {}
但我尝试过的这个版本和其他几个(类似的)版本都被我的g++4.5.2拒绝了。我目前只有一个用于实验。上面写着:
error: could not convert ‘{{i, j}}’ to ‘std::array<int, 2ul>’
这是编译器实现的限制还是发生了什么?
很多时候,问题在于编译器版本。以下代码适用于GCC 4.7.1:
#include <array>
struct X{
std::array<int, 2> a;
X() : a{{1,2}} {}
};
int main(){
X x;
}
活生生的例子。
相关文章:
- 继承:构造函数,初始化C++11中基类的类C数组成员
- 当我在main中声明了我的2d数组时,为什么我的程序会退出
- 如何使用指针传递给函数的数组中对象的函数成员
- 为什么我的排序算法会更改数组值
- 我的目标是编写一个程序来计算和存储字符串在字符数组中出现的位置
- 使用 std::index_sequence 初始化具有固定大小数组成员的 POD 结构容器
- 为什么我的类成员不存在?
- 编译器无法识别我的结构成员
- 我的类的可变参数模板构造函数无法修改我的类成员,为什么会这样?
- 我看到将我的类成员函数指定为内联实际上会增加执行时间,即使函数体非常小
- 如何检查未"self"进程的组成员身份?
- 将规则生产绑定到我的结构成员时出现编译错误
- 为什么我需要一个类似于复合文字的临时构造来初始化我的std::数组成员
- 当我的编译器不符合标准时,如何将数组成员归零
- 实际使用我的类成员函数
- 如何使用c / c ++在OpenLDAP中提取用户的组成员身份
- 为什么我的大5D数组只作为成员变量导致堆栈溢出异常?
- 为什么我的数据成员即使在构造之后也是空的
- C++:如何让不同的类函数操作我的类成员变量
- C++当我离开默认构造函数时,我的初始化字符串消失了,但我的其他成员变量值没有