本文简要说明 explicit的使用示例
explicit 说明符只可出现于在类定义之内的构造函数或转换函数 (C++11 起)的声明说明符序列中。
指定构造函数或转换函数 (C++11 起)或推导指引 (C++17 起)为显式,即它不能用于隐式转换和复制初始化。
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; // OK:复制初始化选择 A::A(int)
A a2(2); // OK:直接初始化选择 A::A(int)
A a3 {4, 5}; // OK:直接列表初始化选择 A::A(int, int) //用C++89编译报error: in C++98 ‘a3’ must be initialized by constructor, not by ‘{...}’
A a4 = {4, 5}; // OK:复制列表初始化选择 A::A(int, int) //用C++89编译报error: in C++98 ‘a4’ must be initialized by constructor, not by ‘{...}’
A a5 = (A)1; // OK:显式转型进行 static_cast
if (a1) ; // OK:A::operator bool()
bool na1 = a1; // OK:复制初始化选择 A::operator bool()
bool na2 = static_cast<bool>(a1); // OK:static_cast 进行直接初始化
// B b1 = 1; // 错误:复制初始化不考虑 B::B(int) //error: conversion from ‘int’ to non-scalar type ‘B’ requested
B b2(2); // OK:直接初始化选择 B::B(int)
B b3 {4, 5}; // OK:直接列表初始化选择 B::B(int, int) //用C++89编译报error: in C++98 ‘b3’ must be initialized by constructor, not by ‘{...}’
// B b4 = {4, 5}; // 错误:复制列表初始化不考虑 B::B(int,int) //error: converting to ‘B’ from initializer list would use explicit constructor ‘B::B(int, int)’
B b5 = (B)1; // OK:显式转型进行 static_cast
if (b2) ; // OK:B::operator bool()
// bool nb1 = b2; // 错误:复制初始化不考虑 B::operator bool() //error: cannot convert ‘B’ to ‘bool’ in initialization
bool nb2 = static_cast<bool>(b2); // OK:static_cast 进行直接初始化
}
示例参考链接: link
将以上代码保存到 cpp_test.c文件中,用C++11标准编译不会报错。
g++ -std=c++11 ./cpp_test.c
用-std=c++98编译会报错:
Google的c++编程风格指南中也规定了explicit的使用时机:(Google的c++编程风格链接: 3.2. 隐式类型转换)