如果我按如下方式初始化std :: array,编译器会给出一个关于缺少大括号的警告
std::array<int, 4> a = {1, 2, 3, 4};
Run Code Online (Sandbox Code Playgroud)
这解决了这个问题:
std::array<int, 4> a = {{1, 2, 3, 4}};
Run Code Online (Sandbox Code Playgroud)
这是警告信息:
missing braces around initializer for 'std::array<int, 4u>::value_type [4] {aka int [4]}' [-Wmissing-braces]
Run Code Online (Sandbox Code Playgroud)
这只是我的gcc版本中的一个错误,还是故意做的?如果是这样,为什么?
我正在快速使用C++ 0x,并使用g ++ 4.6进行测试
我只是尝试了下面的代码,认为它会工作,但它不会编译.我收到错误:
incompatible types in assignment of ‘std::initializer_list<const int>’ to ‘const int [2]’
struct Foo
{
int const data[2];
Foo(std::initializer_list<int const>& ini)
: data(ini)
{}
};
Foo f = {1,3};
Run Code Online (Sandbox Code Playgroud) 我在测试一些关于初始化聚合的问题时遇到了这个问题.我正在使用GCC 4.6.
当我使用列表初始化聚合时,所有成员都是在适当的位置构建的,无需复制或移动.以机智:
int main()
{
std::array<std::array<Goo,2>,2>
a { std::array<Goo,2>{Goo{ 1, 2}, Goo{ 3, 4}} ,
std::array<Goo,2>{Goo{-1,-2}, Goo{-3,-4}} };
}
Run Code Online (Sandbox Code Playgroud)
让我们通过制作一些嘈杂的构造函数来确认:
struct Goo
{
Goo(int, int) { }
Goo(Goo &&) { std::cout << "Goo Moved." << std::endl; }
Goo(const Goo &) { std::cout << "Goo Copied." << std::endl; }
};
Run Code Online (Sandbox Code Playgroud)
运行时,不会打印任何消息.但是,如果我将移动构造函数设为私有,则编译器会抱怨‘Goo::Goo(Goo&&)’ is private,尽管显然不需要移动构造函数.
有没有人知道是否有一个标准的要求移动构造函数可以像这样进行聚合初始化?