Namespaces
Variants

explicit specifier

From cppreference.net
C++ language
General topics
Flow control
Conditional execution statements
Iteration statements (loops)
Jump statements
Functions
Function declaration
Lambda function expression
inline specifier
Dynamic exception specifications ( until C++17* )
noexcept specifier (C++11)
Exceptions
Namespaces
Types
Specifiers
constexpr (C++11)
consteval (C++20)
constinit (C++20)
Storage duration specifiers
Initialization
Expressions
Alternative representations
Literals
Boolean - Integer - Floating-point
Character - String - nullptr (C++11)
User-defined (C++11)
Utilities
Attributes (C++11)
Types
typedef declaration
Type alias declaration (C++11)
Casts
Memory allocation
Classes
Class-specific function properties
explicit (C++11)
static

Special member functions
Templates
Miscellaneous

目录

语法

explicit (1)
explicit ( expression ) (2) (since C++20)
expression - 上下文转换的 bool 类型常量表达式


1) 指定构造函数 或转换函数 (C++11 起) 推导指引 (C++17 起) 为显式,即不能用于 隐式转换 复制初始化
2) explicit 说明符可以与常量表达式一起使用。当且仅当该常量表达式求值为 true 时,该函数为显式函数。
(since C++20)

explicit 说明符仅可在其类定义内的构造函数 或转换函数 (C++11 起) 声明的 声明说明符序列 中出现。

注释

构造函数 具有单个非默认参数 (直至 C++11) ,且未使用函数说明符 explicit 声明时,被称为 转换构造函数

除了 复制 / 移动 构造函数外,其他构造函数和用户定义的转换函数都可以是函数模板; explicit 的含义保持不变。

跟在 explicit 后面的 ( 标记始终被解析为显式说明符的一部分:

struct S
{
    explicit (S)(const S&);    // C++20 中错误,C++17 中正确
    explicit (operator int)(); // C++20 中错误,C++17 中正确
};
(C++20 起)
功能测试宏 标准 功能
__cpp_conditional_explicit 201806L (C++20) 条件性 explicit

关键词

explicit

示例

struct A
{
    A(int) {}      // 转换构造函数
    A(int, int) {} // 转换构造函数 (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;      // 正确:复制初始化选择 A::A(int)
    A a2(2);       // 正确:直接初始化选择 A::A(int)
    A a3 {4, 5};   // 正确:直接列表初始化选择 A::A(int, int)
    A a4 = {4, 5}; // 正确:复制列表初始化选择 A::A(int, int)
    A a5 = (A)1;   // 正确:显式转换执行 static_cast
    if (a1) { }    // 正确:调用 A::operator bool()
    bool na1 = a1; // 正确:复制初始化选择 A::operator bool()
    bool na2 = static_cast<bool>(a1); // 正确:static_cast 执行直接初始化
//  B b1 = 1;      // 错误:复制初始化不考虑 B::B(int)
    B b2(2);       // 正确:直接初始化选择 B::B(int)
    B b3 {4, 5};   // 正确:直接列表初始化选择 B::B(int, int)
//  B b4 = {4, 5}; // 错误:复制列表初始化不考虑 B::B(int, int)
    B b5 = (B)1;   // 正确:显式转换执行 static_cast
    if (b2) { }    // 正确:调用 B::operator bool()
//  bool nb1 = b2; // 错误:复制初始化不考虑 B::operator bool()
    bool nb2 = static_cast<bool>(b2); // 正确:static_cast 执行直接初始化
    [](...){}(a4, a5, na1, na2, b5, nb2); // 抑制“未使用变量”警告
}

参见