【发布时间】:2020-09-26 10:44:40
【问题描述】:
我有别人的代码我不应该改变:
struct Parent { int thing1, thing2; };
并且想让我的继承类可用于constexpr:
struct Child: public Parent
{
constexpr Child() {} //error
constexpr Child(int t1, int t2) { thing1 = t1; thing2 = t2; } //error
constexpr Child(const Child& c) = default;
};
当我在 Visual Studio 中编译它(使用 /std:c++-latest)时,标记的 ctors 会给出错误:
E2433: constexpr constructor must initialized direct base class
它仍然可以编译(尽管将其报告为错误,而不是警告)。它在 g++ 10 中也能正常编译(使用 -std=c++2a)。
(另外,我可以通过显式调用父级的默认 ctor 来消除错误——但我认为这不应该是一个要求?
constexpr Child() : Parent () {}
constexpr Child(int t1, int t2) : Parent () { thing1 = t1; thing2 = t2; }
)
那么,对于 C++20 标准,谁是对的:VS 还是 g++?是否有一种经过批准的方法可以给我的类 constexpr ctors,同时从没有 constexpr ctors 的基类继承(或合并为成员变量)?
【问题讨论】:
标签: c++ visual-c++ c++20