【发布时间】:2020-01-09 21:40:33
【问题描述】:
以下代码在 clang 7+ 中编译,但在 5 和 6 中不编译(使用 c++17 和 c++14)。
clang 5 和 6 的问题似乎是隐式复制 ctor 从可变成员 x 读取。
谁能告诉我,整个结构是否符合标准(c++17),或者程序是否格式错误?还是在较早的 clang 版本中可能无法实现的隐式 copy-ctor 的标准发生了变化?
struct Foo {
int a;
mutable int x{};
constexpr Foo() : a(0) {}
//constexpr Foo(const Foo& other) : a(other.a) {} // <- with this line it works on Clang 5 & 6, too
};
struct FooFactory {
static constexpr auto create() {
auto f = Foo{};
return f;
}
};
int main() {
constexpr Foo f = FooFactory::create();
++f.x;
}
直播代码here.
【问题讨论】:
标签: c++ language-lawyer c++17 copy-constructor constexpr