g++ 4.9 拒绝 C++14 中的有效聚合初始化
g++ 4.9 rejects valid aggregate initialization in C++14
考虑这段代码:
struct S
{
int x;
double y = 1.1;
};
int main()
{
S s = {0};
}
根据 C++14 标准,§ 8.5.1/7
If there are fewer initializer-clauses in the list than there are members in the aggregate, then each member not explicitly initialized shall be initialized from its brace-or-equal-initializer or, if there is no brace-or-equal- initializer, from an empty initializer list (8.5.4).
代码应该完全有效。
但是,g++ 4.9.2 拒绝代码(使用 -std=c++14
编译)
so.cpp:9:13: error: could not convert '{0}' from '<brace-enclosed initializer list>' to 'S'
S s = {0};
clang++ 另一方面编译它。
这是 g++ 的已知问题吗?
你是对的,这是有效的 C++14;然而,在 C++11 中 class 和 in class member initializers was not an aggregate 所以这在 C++11 中是无效的。
我在回答上述问题时指出的问题是 gcc did not support this change until 5.0 (see it live):
G++ now supports C++14 aggregates with non-static data member
initializers.
struct A { int i, j = i; };
A a = { 42 }; // a.j is also 42
考虑这段代码:
struct S
{
int x;
double y = 1.1;
};
int main()
{
S s = {0};
}
根据 C++14 标准,§ 8.5.1/7
If there are fewer initializer-clauses in the list than there are members in the aggregate, then each member not explicitly initialized shall be initialized from its brace-or-equal-initializer or, if there is no brace-or-equal- initializer, from an empty initializer list (8.5.4).
代码应该完全有效。
但是,g++ 4.9.2 拒绝代码(使用 -std=c++14
编译)
so.cpp:9:13: error: could not convert '{0}' from '<brace-enclosed initializer list>' to 'S'
S s = {0};
clang++ 另一方面编译它。
这是 g++ 的已知问题吗?
你是对的,这是有效的 C++14;然而,在 C++11 中 class 和 in class member initializers was not an aggregate 所以这在 C++11 中是无效的。
我在回答上述问题时指出的问题是 gcc did not support this change until 5.0 (see it live):
G++ now supports C++14 aggregates with non-static data member initializers.
struct A { int i, j = i; }; A a = { 42 }; // a.j is also 42