谈谈explicit关键字

本文详细解释了C++中explicit关键字的作用及应用场景,通过具体示例介绍了如何避免隐式转换导致的问题,并探讨了构造函数与赋值操作符的默认行为。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

今天看到公司的代码内有大量的explicit关键字,但是老版的MSDN内例子并不完善,实在是不明白,最终从网上一篇文章内找到了答案:原来explicit是为了防止隐式使用拷贝构造函数的.以下附上从新版MSDN中找到的例子和网上那篇文章:

// Copy From MSDN

This keyword is a declaration specifier that can only be applied to in-class constructor declarations. An explicit constructor cannot take part in implicit conversions. It can only be used to explicitly construct an object.

The following program will fail to compile because of the explicit keyword. To resolve the error, remove the explicit keywords and adjust the code in g.

// spec1_explicit.cpp
// compile with: /EHsc
#include 
class C 
{
public:
   int i;
   explicit C(const C&)   // an explicit copy constructor
   {
      printf("/nin the copy constructor");
   }
   explicit C(int i )   // an explicit constructor
   {
      printf("/nin the constructor");
   }


   C()
   {
      i = 0;
   }
};

class C2
{
public:
   int i;
   explicit C2(int i )   // an explicit constructor
   {
   }
};

C f(C c)
{   // C2558
   c.i = 2;
   return c;   // first call to copy constructor
}

void f2(C2)
{
}

void g(int i)
{
    f2(i);   // C2558
   // try the following line instead
   // f2(C2(i));
}

int main()
{
   C c, d;
   d = f(c);   // c is copied
}
Note   explicit on a constructor with multiple arguments has no effect, since such constructors cannot take part in implicit conversions. However, for the purpose of implicit conversion, explicit will have an effect if a constructor has multiple arguments and all but one of the arguments has a default value.

 

// Copy From Internet Article

Pointer
不看书不知道自己的C++有多差T_T,看到 explicit 时遇到一个问题。请看下面一段程序:
class A{
public:
A(int i) : m_i(i){}
int m_i;
};
int main(){
A a = 0;
a = 10; // 这里是什么操作?
}

这个操作产生了一个临时对象。
我怀疑是默认赋值运算符 “A &operator = (int i){}”,于是重载了一下该运算符,结果确实运行到重载的运算符函数里了,那么临时对象又是如何产生的呢?

难道默认的赋值运算符是这样操作的?
A &operator = (int i){
A a(i);
return a;
}

这让我想起了类似的函数操作:
void fn(A a){
// ...
}
这里可以直接写fn(10);也是产生了一个临时对象。

难道真的是这样吗?忘解惑。

alexeyomux
老兄你用的是哪个编译器?在我印象之中,好像标准C++并不会给出operator =啊。等我去试一试。

gongminmin
当然会有默认的operator=了
按照c++标准,编译器会生成五个默认成员函数:
默认构造函数
拷贝构造函数
析构函数
operator=
operator&
 
千里马肝
class A
{
public:
A(int i) : m_i(i){}
int m_i;
};

分别说说吧:
1. A a = 0;
首先, compiler认为这样写是不符合规矩的, 因为A = A才是正常行为。
但是她并不放弃, 通过搜索, 发现A可以根据一个int构造, 同时这个A(int i)没有用explicit修饰过。
那么A a = 0; 这样的一句话随即转变成:
A tmp(0);
A a = tmp;
需要说明的是, A a = tmp是调用的copy ctor, 虽然class A中并没有, 但是通常不写copy ctor的话,
compiler都会生成一个memberwise assignment操作性质的ctor, 底层实现通常会以memcpy进行。

2. a = 10;
首先, 这样同ctor的情况一样, compiler无法直接进行操作。
类推, 等同于代码:
A tmp(10);
a = tmp;
需要注意的是, a = tmp是调用的assignment操作, 同ctor一样,我们自己不写, 编译器同样进行
memberwise assignment操作。

3. fn(A a)
同样, fn(10)也是不对的, 但是"按照惯例", 呵呵, 会有:
A tmp(10);
fn(tmp);

另外, 为你解惑:
copy ctor的写法只能是T::T(const T &);
而assignment的写法可以多变, 即任意. 以T为例,
可以有
T &operator = (int n);
也可有
T &operator = (const char *);
当然, 你要确认如此的定义是对T而言有意义.

然后, 上述a = tmp, 即调用的默认的、标准的、自动生成的T &operator = (const T &).
开销是会有一个临时的A tmp生成, 然后memcpy.
但如果你自已写了T &operator = (int n), 那么a = 10即意味着a.m_i = 10.
当然, 以开销而言要视你的T &operator = (int n)是否为inline了.

对于explicit, 当修饰explicit A(int i) : m_i(i){}, 那么即告诉compiler不要在私底下做那么多的转换动作.
而且自动生成如A tmp(0)这样的东西是我们不想要的, 因为某些情况下自动转换这种行为是错误的.

最后, 相关此类问题, 还有一个话题, 即class A可以有operator int(), 会在
fn(int n){}
A a(3);
fn(a)
起到魔术般的作用. 关于这个, 留给你自己看看书吧:)

最后,祝学习C++的路上一帆风顺。Good luck~
<think>我们正在讨论C++中的explicit关键字。根据用户提供的引用内容,explicit关键字主要用于防止构造函数的隐式类型转换,以提高代码的安全性和可读性。下面我将详细解释explicit关键字的用途和使用场景。 ### 1. explicit关键字的核心作用 - **阻止隐式转换**:当构造函数只有一个参数,或者有多个参数但除了第一个参数外都有默认值时,该构造函数可能被编译器用于隐式类型转换。使用explicit关键字可以阻止这种隐式转换,要求必须显式调用构造函数。 - **避免歧义**:防止因隐式转换导致的代码逻辑错误或性能问题(如不必要的临时对象构造)。 ### 2. 使用场景 #### 场景1:单参数构造函数 ```cpp class MyString { public: // 不加explicit:允许隐式转换 MyString(const char* str) { /* ... */ } }; void printString(const MyString& s) { /* ... */ } // 调用 printString("Hello"); // 隐式转换:将const char*转换为MyString对象 ``` 若在构造函数前加`explicit`: ```cpp explicit MyString(const char* str) { /* ... */ } // printString("Hello"); // 错误:不能隐式转换 printString(MyString("Hello")); // 正确:显式转换 ``` #### 场景2:转换运算符(C++11起) ```cpp class BoolWrapper { public: // explicit修饰转换运算符 explicit operator bool() const { return true; } }; BoolWrapper wrapper; // if (wrapper) { ... } // 错误:不能隐式转换为bool if (static_cast<bool>(wrapper)) { ... } // 正确:显式转换 ``` ### 3. 隐式转换的风险示例 考虑一个表示文件路径的类: ```cpp class FilePath { public: FilePath(const std::string& path) : path_(path) {} private: std::string path_; }; void openFile(const FilePath& path) { /* ... */ } // 调用 openFile("data.txt"); // 隐式转换:构造临时FilePath对象 ``` 若`std::string`的构造函数也是非explicit的,则可能发生多重隐式转换,增加歧义。 ### 4. 何时使用explicit? - **构造函数可能引起歧义时**:如`std::vector`的构造函数`vector(size_type n)`会创建n个默认元素,但若传入一个整数,可能被误认为初始化列表。因此标准库中该构造函数为explicit。 - **避免意外构造临时对象**:防止隐式转换导致的性能损耗(构造和析构临时对象)或逻辑错误。 - **增强代码可读性**:显式调用构造函数使代码意图更清晰。 ### 5. 代码对比 ```cpp // 无explicit:允许隐式转换 class Implicit { public: Implicit(int x) { } }; void foo(Implicit obj) {} foo(42); // 合法:隐式构造Implicit(42) // 有explicit:必须显式调用 class Explicit { public: explicit Explicit(int x) { } }; void bar(Explicit obj) {} // bar(42); // 错误 bar(Explicit(42)); // 合法 ``` ### 6. 总结 | 特性 | 隐式转换(无explicit) | 显式转换(有explicit) | |--------------|-----------------------------|-----------------------------| | **构造函数调用** | `Type obj = value;` 合法 | 必须显式:`Type obj(value);` | | **函数参数传递** | 自动构造临时对象 | 需手动转换或构造 | | **转换运算符** | 自动调用转换 | 需`static_cast`显式转换 | > **最佳实践**:除非有明确理由允许隐式转换,否则将单参数构造函数和转换运算符声明为`explicit`[^1][^2][^3][^4][^5]。 §§相关问题§§ 1. 在C++11及以后的标准中,explicit关键字有哪些增强? 2. 如何避免由隐式转换导致的性能问题? 3. 在模板类中使用explicit关键字有哪些注意事项? 4. explicit关键字能否用于类模板的构造函数?</think>### C++ `explicit` 关键字详解 #### 1. **核心作用:阻止隐式转换** `explicit` 关键字专门用于修饰**单参数构造函数**或**转换运算符**,强制要求必须进行**显式类型转换**,禁止编译器自动执行隐式转换。 ```cpp // 非 explicit 构造函数(允许隐式转换) class Implicit { public: Implicit(int x) {} // 允许 int -> Implicit 的隐式转换 }; // explicit 构造函数(禁止隐式转换) class Explicit { public: explicit Explicit(int x) {} // 必须显式调用 }; void test() { Implicit obj1 = 42; // ✅ 合法:隐式转换 Explicit obj2 = 42; // ❌ 错误:不能隐式转换 Explicit obj3(42); // ✅ 合法:显式构造 Explicit obj4 = Explicit(42); // ✅ 合法:显式构造 } ``` #### 2. **关键使用场景** ##### (1) 防止构造函数歧义 ```cpp class Vector { public: explicit Vector(size_t n); // 显式声明:避免误用为初始化列表 Vector(std::initializer_list<int>); }; Vector v1 = 10; // ❌ 错误:explicit 阻止 size_t -> Vector Vector v2(10); // ✅ 合法:显式构造(创建10个元素的vector) Vector v3{1,2,3}; // ✅ 合法:初始化列表构造 ``` ##### (2) 避免意外类型转换 ```cpp class Timer { public: explicit Timer(int seconds); // 禁止 int -> Timer 隐式转换 }; void schedule(Timer t); schedule(5); // ❌ 错误:不能隐式转换 schedule(Timer(5)); // ✅ 合法:显式构造 ``` ##### (3) 转换运算符(C++11起) ```cpp class SafeBool { public: explicit operator bool() const { // 必须显式转换为bool return isValid(); } }; SafeBool sb; if (sb) {...} // ❌ 错误:不能隐式转换 if (static_cast<bool>(sb)) // ✅ 合法:显式转换 ``` #### 3. **隐式转换的风险案例** ```cpp // 问题代码:非 explicit 构造函数 class File { public: File(const string& name); // 允许隐式转换 }; void saveTo(File f); saveTo("backup.txt"); // 隐式转换:string -> File // 实际行为:构造临时File对象 // 可能导致资源泄露或逻辑错误 ``` 使用 `explicit` 修复: ```cpp explicit File(const string& name); // 禁止隐式转换 // saveTo("backup.txt"); // ❌ 编译错误 saveTo(File("backup.txt")); // ✅ 明确资源创建 ``` #### 4. **何时必须使用 explicit?** | 场景 | 是否推荐使用 | 原因 | |-------------------------------|--------------|----------------------------------------------------------------------| | 单参数构造函数 | ✅ 强烈推荐 | 避免意外类型转换引发的逻辑错误 | | 多参数构造函数(含默认参数) | ✅ 推荐 | `ClassName(int a, int b=0)` 等效于单参数构造 | | 转换运算符 (`operator type()`) | ✅ 推荐 | 防止在条件语句中意外触发转换 | | POD类型(简单数据容器) | ⚠️ 谨慎使用 | 可能过度限制合理的使用场景 | #### 5. **标准库应用实例** C++ 标准库广泛使用 `explicit`: ```cpp // std::unique_ptr explicit unique_ptr(pointer p); // 禁止指针隐式转为智能指针 // std::vector explicit vector(size_type count); // 避免误用:vector<int> v = 10; ``` > **设计哲学**:`explicit` 的核心价值在于**提升代码安全性**和**明确开发者意图**,通过编译时错误阻止潜在的运行时错误[^1][^2][^3][^4][^5]。 --- ### 常见问题解答 **Q:为什么默认构造函数不需要 explicit?** A:默认构造无参数,不存在隐式转换场景。 **Q:explicit 是否影响显式类型转换?** A:不影响。`static_cast`、C风格转换等显式操作仍可正常使用。 **Q:模板构造函数能否用 explicit?** A:可以。`template<typename T> explicit MyClass(T&& arg);` ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值