小编NKA*_*KAR的帖子

显式 (bool) 的用例是什么

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

c++ explicit c++20

23
推荐指数
3
解决办法
1795
查看次数

标签 统计

c++ ×1

c++20 ×1

explicit ×1