gpt4 book ai didi

c++ - g++ 4.9 拒绝 C++14 中的有效聚合初始化

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:36:01 24 4
gpt4 key购买 nike

考虑这段代码:

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 中,一个带有 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

关于c++ - g++ 4.9 拒绝 C++14 中的有效聚合初始化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28113750/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com