【发布时间】:2016-01-22 17:29:39
【问题描述】:
这段代码对 C++14 有效吗
using namespace std;
struct Point
{
int x = 0;
int y = 0;
};
Point p2 {1, 1};
它在 clang++ 7.0 下编译得很好,在这两种情况下它都不适用于 G++ 4.9 我将 --std=c++1y 传递给编译器。
在 G++ 中,当我从结构定义中删除默认值时,它会起作用。
g++ test_constexpr_ctor.cc --std=c++1y -o test
test_constexpr_ctor.cc:7:15: error: no matching function for call to ‘Point::Point(<brace-enclosed initializer list>)’
Point p2 {1, 1};
^
test_constexpr_ctor.cc:7:15: note: candidates are:
test_constexpr_ctor.cc:1:8: note: constexpr Point::Point()
struct Point
^
test_constexpr_ctor.cc:1:8: note: candidate expects 0 arguments, 2 provided
test_constexpr_ctor.cc:1:8: note: constexpr Point::Point(const Point&)
test_constexpr_ctor.cc:1:8: note: candidate expects 1 argument, 2 provided
test_constexpr_ctor.cc:1:8: note: constexpr Point::Point(Point&&)
test_constexpr_ctor.cc:1:8: note: candidate expects 1 argument, 2 provided
【问题讨论】:
-
很可能是 gcc 4.9 错误。
-
是老g++的bug。将其更新到较新的版本并成功编译。
-
@VictorPolevoy 你测试了哪个 G++ 版本?
-
g++ 5.1.0就够了。 -
@101010 是的,这是一个错误,请参阅 C++11 aggregate initialization for classes with non-static member initializers 和 g++ 4.9 rejects valid aggregate initialization in C++14 ... 最后看起来像重复