为什么这不起作用:
struct Base {
Base(int a, int b) {}
};
struct Derived: Base {
// using Base::Base; // unless I add this
};
int main() {
Derived d(0, 0);
}
Run Code Online (Sandbox Code Playgroud)
虽然这可以:
struct Base {
Base(int a) {}
};
struct Derived: Base {
// using Base::Base; // without this line
};
int main() {
Derived d(0);
}
Run Code Online (Sandbox Code Playgroud)
注意:C++20 GCC 10.2(第二个示例也不适用于 C++17)
C++20 中第二个例子背后的魔力是什么?
我正在尝试使我的程序多平台,最初是为 Linux 编写的。MSVC(我使用的是 19.28)被告知从 19.20 版(https://en.cppreference.com/w/cpp/compiler_support/20)获得飞船操作员支持,但它似乎没有为 std 定义这个操作符::string 或 std::shared_ptr(可能适用于许多其他人)。
我真正想做的是:
// int x(1), y(2); // ok!
// std::string x("1"), y("2"); // nope
std::shared_ptr<int> x(new int), y(new int); // nope
auto r = x <=> y; // error C2676 for string and shared_ptr
Run Code Online (Sandbox Code Playgroud)
实例:https : //godbolt.org/z/Eo49hh
它在 GCC 10.2 下工作。我是否在这里遗漏了某些点,或者它没有得到完全支持?