C++ 关键字 explicit

explicit 说明符 cppreference

explicit 	(1)
explicit ( 表达式 ) 	(2) 	(C++20 起)
  1. 指定构造函数或转换函数 (C++11 起)或推导指引 (C++17 起)为显式,即它不能用于隐式转换和复制初始化。

  2. explicit 说明符可以与常量表达式一同使用。当且仅当该常量表达式求值为 true 时函数为显式。(C++20 起)

explicit 说明符只可出现于在类定义之内的构造函数或转换函数 (C++11 起)的 decl-specifier-seq(声明说明符序列) 中。

注解

声明时不带函数说明符 explicit 的拥有单个无默认值形参的 (C++11 前)构造函数被称作转换构造函数。

构造函数(除了复制/移动)和用户定义转换函数都可以是函数模板;explicit 的含义不变。

跟随 explicit 的 ( 记号被剖析成 explicit 指定符的一部分:

struct S {
    explicit (S)(const S&);    // C++20 中错误,C++17 中 OK
    explicit (operator int)(); // C++20 中错误,C++17 中 OK
};

示例

struct A
{
    A(int) { }      // converting constructor
    A(int, int) { } // converting constructor (C++11)
    operator bool() const { return true; }
};

struct B
{
    explicit B(int) { }
    explicit B(int, int) { }
    explicit operator bool() const { return true; }
};

int main()
{
    A a1 = 1;      // OK: copy-initialization selects A::A(int)
    A a2(2);       // OK: direct-initialization selects A::A(int)
    A a3 {4, 5};   // OK: direct-list-initialization selects A::A(int, int)
    A a4 = {4, 5}; // OK: copy-list-initialization selects A::A(int, int)
    A a5 = (A)1;   // OK: explicit cast performs static_cast
    if (a1) ;      // OK: A::operator bool()
    bool na1 = a1; // OK: copy-initialization selects A::operator bool()
    bool na2 = static_cast<bool>(a1); // OK: static_cast performs direct-initialization

//  B b1 = 1;      // error: copy-initialization does not consider B::B(int)
    B b2(2);       // OK: direct-initialization selects B::B(int)
    B b3 {4, 5};   // OK: direct-list-initialization selects B::B(int, int)
//  B b4 = {4, 5}; // error: copy-list-initialization does not consider B::B(int,int)
    B b5 = (B)1;   // OK: explicit cast performs static_cast
    if (b2) ;      // OK: B::operator bool()
//  bool nb1 = b2; // error: copy-initialization does not consider B::operator bool()
    bool nb2 = static_cast<bool>(b2); // OK: static_cast performs direct-initialization
}
posted @ 2020-06-03 14:25  菁芜  阅读(351)  评论(0编辑  收藏  举报