C++中的explicit关键字

本文深入探讨了C++中explicit关键字的使用,解释了其如何防止构造函数的隐式转换和复制初始化,提高了代码的可读性和安全性。

 

转自:https://blog.csdn.net/tianmingdyx/article/details/79823470



1 莫名其妙的explicit

 

    在很多C++代码中,定义类的构造函数的时候,往往会在前面加一个explicit关键字。先来看cppreference上面对它的解释:

The explicit specifier specifies that a constructor or conversion function (since C++11) doesn't allow implicit conversions or copy-initialization. It may only appear within the decl-specifier-seq of the declaration of such a function within its class definition.

    大体意思是说,用explicit修饰构造函数的作用是禁止隐式转换或复制初始化。那什么是隐式转换和复制初始化呢?这要从类的构造函数和初始化过程说起。

 

2 构造函数和初始化过程

 

    在C++中,变量的初始化有两种方式:直接初始化(用()运算符,如int a(1);)和复制初始化(用=运算符,如int a = 1;)。在编译和代码运行过程中,这两种初始化是有区别的。先来看一个例子:

 

// 实例2
#include<iostream>

using namespace std;

class A
{
public:
    A(){ cout << "f1" << endl;}                    // 无参数构造函数,记为f1
    A(int temp){ cout << "f2" << endl; }         // 单参数构造函数,记为f2
    A(const A&  temp){ cout << "f3" << endl;}   // 复制构造函数,记为f3
private:
    int a;
};

int main()
{
    A a1;               // 调用f1
    A a2(1);            // 调用f2
    A a3(a2);           // 调用f3
    cout << "------" << endl;
    A a4 = 1;           // 隐式转换
    cout << "------" << endl;
    A a5 = a2;          // 拷贝初始化
    return 0;
}

    实例1代码可以编译通过并运行,运行结果如下:(gcc6.3)

 

f1

f2

f3

------

f2

------

f3

 

    从运行结果可以看出,在直接初始化(即main函数的第二三句代码)过程中,会根据()中的数据的类型去调用不同构造函数,其中就包括复制构造函数。A a4 = 1和A a5 = a2这两句代码是复制初始化。但是其中的A a4 = 1这句代码令人困惑,为什么一个数字可以初始化一个类对象呢?其实这个地方发生了隐式转换。

 

3 隐式转换

 

    看一下cppreference中对隐式转换的解释:

 

Implicit conversions are performed whenever an expression of some type T1 is used in context that does not accept that type, but accepts some other type T2; in particular:

* when the expression is used as the argument when calling a function that is declared with T2 as parameter;

* when the expression is used as an operand with an operator that expects T2;

* when initializing a new object of type T2, including return statement in a function returning T2;

* when the expression is used in a switch statement (T2 is integral type);

* when the expression is used in an if statement or a loop (T2 is bool).

 

    其中高亮的那一条,指的就是我们在实例1中遇到的A a4 = 1情况。通过代码运行的结果我们可以看出,上面这两句实际上被编译器隐式的转换成了下面的过程。(具体的过程可能跟编译器有关,有资料说A a4 = 1会先调用f2,构造出一个临时变量temp,再调用f3将temp复制给a4,带这里实际测试结果是只调用的f2)

 

A a4(1);

A a5(a2);

   

    虽然上面的代码是可以正常运行的,但是像A a4 = 1这样的代码的可读性非常的差,因此,为了避免出现这种情况,我们可以使用explicit关键字来修饰构造函数,从而禁止隐式转换和复制初始化。

 

4 使用explicit关键字

 

    将实例1的代码做如下的修改:

 

// 实例2
#include<iostream>

using namespace std;

class A
{
public:
    A(){ cout << "f1" << endl;}                    // 无参数构造函数,记为f1
    explicit A(int temp){ cout << "f2" << endl; }         // 单参数构造函数,记为f2
    explicit A(const A&  temp){ cout << "f3" << endl;}   // 复制构造函数,记为f3
private:
    int a;
};

int main()
{
    A a1;               // 调用f1
    A a2(1);            // 调用f2
    A a3(a2);           // 调用f3
    cout << "------" << endl;
    A a4 = 1;           // 隐式转换
    cout << "------" << endl;
    A a5 = a2;          // 拷贝初始化

    return 0;
}

    在单参数构造函数和拷贝构造函数之前加上explicit关键字,然后再编译代码。这时,编译不能通过,并会报出如下的错误:

 

prog.cpp: In function ‘int main()’:

prog.cpp:22:12: error: conversion from ‘int’ to non-scalar type ‘A’ requested

A a4 = 1; // 隐式转换

^

prog.cpp:24:12: error: no matching function for call to ‘A::A(A&)’

A a5 = a2; // 拷贝初始化

 

这就是因为explicit关键字禁止了隐式转换和复制初始化,因此编译不能通过。注释掉A a4 = 1和A a5 = a2这两句代码就可以编译通过了。 通过使用explicit关键字,可以强制性的要求类的使用者按照规范的方式编写代码,提高代码的可读性,并降低发生错误的风险。

 

注意

 

  • 复制构造函数又被叫做拷贝构造函数(copy constructor),复制初始化也叫做拷贝初始化。
  • 拷贝构造函数和拷贝初始化不要混淆。拷贝初始化过程要调用拷贝构造函数,但拷贝构造函数也可以用在直接初始化过程。
  • explicit关键字只对单形参的构造函数起作用,对于多形参的构造函数,不会出现隐式转换的问题。 《C++ Primer》中有指出“可以用单个实参来调用的构造函数定义了从形参类型到该类型的一个隐式转换。”
  • 隐式转换不仅会出现在初始化过程中,另一个很常见的情况是以类类型为形参的函数的参数传递过程,比如下面的代码,如果加上explicit关键字,是不能编译通过的。
// 实例3
#include<iostream>

using namespace std;

class A
{
public:
    A(){ cout << "f1" << endl;}                    // 无参数构造函数,记为f1
     A(int temp){ cout << "f2" << endl; }         // 单参数构造函数,记为f2
     A(const A&  temp){ cout << "f3" << endl;}   // 复制构造函数,记为f3
    int x;
};

A fun(A a){a.x = 1; return a;}

int main()
{
    A ret;
    cout << "----------" << endl;
    ret = fun(1);
    return 0;
}

运行结果:

 

f1

----------

f2

f3

### C++ 中 `explicit` 关键字的作用和用法 #### 单参数构造函数中的使用 在C++中,`explicit`关键字主要用于防止编译器执行不必要的隐式类型转换。当定义了一个只接受单一参数的构造函数时,如果不加`explicit`修饰,则该构造函数可以被用来实现从其参数类型的对象到类类型的隐式转换。这可能会导致一些难以察觉的错误。 通过声明为`explicit`,能够阻止这些潜在危险的行为发生,使得只有显式的强制转换才能触发此类构造过程[^1]: ```cpp class MyClass { public: // 不带 explicit 的构造函数允许隐式转换 MyClass(int value); }; void func(MyClass obj); int main() { int num = 5; // 下面这一行会因为存在无参构造而成功编译并运行, // 导致意料之外的对象创建行为。 func(num); } ``` 如果我们将上述例子中的构造函数改为带有`explicit`的关键字形式: ```cpp class MyClass { public: // 带有 explicit 的构造函数不允许隐式转换 explicit MyClass(int value); }; ``` 此时再尝试传递整数给期望接收`MyClass`实例的地方将会引发编译期报错,除非程序员明确指定了类型转换操作。 #### 转换运算符上的应用 除了应用于单个参数的构造函数外,`explicit`也可以标注于用户自定义类型之间的转换运算符上。同样地,这样做是为了避免不希望发生的自动转型情况的发生: ```cpp class IntWrapper { private: int m_value; public: // 显式指定此转换仅能由显示转换完成 explicit operator bool() const noexcept { return static_cast<bool>(m_value); } }; ``` 在这个案例里,即使有一个布尔上下文环境(比如条件判断),也不会让`IntWrapper`对象直接参与逻辑表达式的求值;相反,必须采用静态或动态方式来实施必要的转变动作。 关于多参数构造的情况,并不在`explicit`的应用范围内,因此对于这种情况下的处理方法并不涉及`explicit`关键字本身[^2]。然而值得注意的是,在某些版本之后的标准库支持下,可以通过大括号语法来进行初始化列表风格的新建工作,但这属于另一个话题了。
<think>我们正在讨论C++中的explicit关键字。该关键字用于修饰构造函数,防止编译器进行隐式类型转换,以避免意外的类型转换发生。 通常,当构造函数只有一个参数(或者有多个参数,但除了第一个参数外都有默认值)时,编译器可能会使用该构造函数进行隐式转换。 使用explicit关键字可以防止这种隐式转换,只能进行显式转换。 下面我们通过示例代码来说明。</think>### C++中`explicit`关键字的作用详解 `explicit`是C++中用于修饰构造函数的特殊关键字,其主要作用是**禁止编译器执行隐式类型转换**,确保类型转换只能通过显式方式进行。 #### 核心作用: 1. **防止隐式类型转换**:避免编译器自动调用构造函数进行不期望的类型转换 2. **提高代码安全性**:消除可能导致意外行为的隐式转换 3. **增强代码可读性**:明确标识需要显式调用的构造函数 ### 代码示例分析 #### 示例1:隐式转换的问题 ```cpp class Distance { public: // 允许隐式转换的构造函数 Distance(double meters) : m_meters(meters) {} void show() const { std::cout << "Distance: " << m_meters << " meters\n"; } private: double m_meters; }; int main() { Distance d1 = 5.0; // 隐式转换:double -> Distance ✅ Distance d2 = 100; // 隐式转换:int -> Distance ✅ d1 = 3.14; // 隐式转换:double -> Distance ✅ d2 = 200; // 隐式转换:int -> Distance ✅ d1.show(); // 输出:Distance: 3.14 meters d2.show(); // 输出:Distance: 200 meters // 可能意外发生的情况 if (d1 == 10.0) { // 隐式转换:double -> Distance ❌ // 这个比较可能不是开发者本意 } return 0; } ``` #### 示例2:使用`explicit`解决隐式转换问题 ```cpp class SafeDistance { public: // 使用explicit禁止隐式转换 explicit SafeDistance(double meters) : m_meters(meters) {} void show() const { std::cout << "SafeDistance: " << m_meters << " meters\n"; } private: double m_meters; }; int main() { // SafeDistance sd1 = 5.0; // 错误:不能隐式转换 ❌ // SafeDistance sd2 = 100; // 错误:不能隐式转换 ❌ SafeDistance sd1(5.0); // 正确:显式构造 ✅ SafeDistance sd2(100); // 正确:显式构造 ✅ // sd1 = 3.14; // 错误:不能隐式赋值 ❌ sd1 = SafeDistance(3.14); // 正确:显式转换 ✅ sd2 = static_cast<SafeDistance>(200); // 正确:显式转换 ✅ sd1.show(); // 输出:SafeDistance: 3.14 meters sd2.show(); // 输出:SafeDistance: 200 meters // 避免意外比较 // if (sd1 == 10.0) { // 错误:不能隐式转换 ❌ // // 编译错误,防止意外行为 // } return 0; } ``` ### 关键应用场景 1. **单参数构造函数**: ```cpp class StringWrapper { public: explicit StringWrapper(const char* str) : data(str) {} private: std::string data; }; // StringWrapper s = "hello"; // 错误 StringWrapper s("hello"); // 正确 ``` 2. **多参数构造函数(C++11起)**: ```cpp class Point { public: explicit Point(int x, int y) : x(x), y(y) {} private: int x, y; }; // Point p = {1, 2}; // 错误 Point p(1, 2); // 正确 ``` 3. **转换运算符(C++11起)**: ```cpp class SmartBool { public: explicit operator bool() const { return value; } private: bool value; }; SmartBool sb; // if (sb) {...} // 错误 if (static_cast<bool>(sb)) {...} // 正确 ``` ### 何时使用`explicit` | 场景 | 是否推荐使用`explicit` | 原因 | |------|------------------------|------| | 单参数构造函数 | ✅ 强烈推荐 | 防止意外类型转换 | | 多参数构造函数 | ✅ 推荐 | 防止列表初始化意外转换 | | 转换函数 | ✅ 推荐 | 防止意外布尔转换 | | 复制构造函数 | ❌ 不推荐 | 会禁用复制初始化 | | 移动构造函数 | ❌ 不推荐 | 会禁用移动初始化 | ### 最佳实践建议 1. **默认使用`explicit`**: ```cpp // 好的实践:默认添加explicit class SafeClass { public: explicit SafeClass(int value); }; ``` 2. **仅在需要隐式转换时省略**: ```cpp // 特殊场景:设计需要隐式转换 class ImplicitConverter { public: ImplicitConverter(int value); // 允许隐式转换 }; ``` 3. **结合`delete`禁用特定转换**: ```cpp class SpecialNumber { public: explicit SpecialNumber(int value); SpecialNumber(double) = delete; // 禁用double转换 }; ``` ### 总结 `explicit`关键字C++类型安全的重要工具: 1. 防止不期望的隐式类型转换 2. 使代码意图更明确 3. 减少运行时错误 4. 提高代码可维护性
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值