C++20 引入了显式 (bool),它在编译时有条件地选择构造函数是否显式。
下面是我在这里找到的一个例子。
struct foo {
// Specify non-integral types (strings, floats, etc.) require explicit construction.
template <typename T>
explicit(!std::is_integral_v<T>) foo(T) {}
};
foo a = 123; // OK
foo b = "123"; // ERROR: explicit constructor is not a candidate (explicit specifier evaluates to true)
foo c {"123"}; // OK
Run Code Online (Sandbox Code Playgroud)
谁能告诉我explicit (bool)
除了 using 之外的任何其他用例std::is_integral
?