Comments (2)
很棒了,只是「错误:被 explicit 修饰的构造函数不可以从 int 到 B 的隐式转换」这句话需要注意一下,阅读起来不大通顺。除此之外就没有建议了,这个 Issue 就关闭了哈~
from interview.
感谢提出关于 explicit 的改进,学习了。
我之前没了解过按语境转换,对您说的「这样的例外使得在 if 等需要隐式将右值转换为 bool 的场景下,即使转换函数被声明为 explicit 的,隐式转换仍会发生。」不是很理解,请问是指如下例子中if (b1);
和bool b6(b1);
的意思吗?
struct A
{
A(int) { }
operator bool() const { return true; }
};
struct B
{
explicit B(int) {}
explicit operator bool() const { return true; }
};
void doA(A a) {}
void doB(B b) {}
int main()
{
A a1(1); // OK:直接初始化
A a2 = 1; // OK:复制初始化
A a3{ 1 }; // OK:直接列表初始化
A a4 = { 1 }; // OK:复制列表初始化
A a5 = (A)1; // OK:允许 static_cast 的显式转换
doA(1); // OK:允许从 int 到 A 的隐式转换
if (a1); // OK:使用转换函数 A::operator bool() 的从 A 到 bool 的隐式转换
bool a6(a1); // OK:使用转换函数 A::operator bool() 的从 A 到 bool 的隐式转换
bool a7 = a1; // OK:使用转换函数 A::operator bool() 的从 A 到 bool 的隐式转换
bool a8 = static_cast<bool>(a1); // OK :static_cast 进行直接初始化
B b1(1); // OK:直接初始化
B b2 = 1; // 错误:被 explicit 修饰的构造函数不可以复制初始化
B b3{ 1 }; // OK:直接列表初始化
B b4 = { 1 }; // 错误:被 explicit 修饰的构造函数不可以复制列表初始化
B b5 = (B)1; // OK:允许 static_cast 的显式转换
doB(1); // 错误:被 explicit 修饰的构造函数不可以从 int 到 B 的隐式转换
if (b1); // OK:被 explicit 修饰的转换函数 B::operator bool() 可以从 B 到 bool 的按语境转换
bool b6(b1); // OK:被 explicit 修饰的转换函数 B::operator bool() 可以从 B 到 bool 的按语境转换
bool b7 = b1; // 错误:被 explicit 修饰的转换函数 B::operator bool() 不可以隐式转换
bool b8 = static_cast<bool>(b1); // OK:static_cast 进行直接初始化
return 0;
}
另外,explicit修饰构造函数也可以防止隐式转换,因此我修改如下:
- explicit 修饰构造函数时,可以防止隐式转换和复制初始化
- explicit 修饰转换函数时,可以防止隐式转换,但 按语境转换 除外
我对这方面了解的没这么细,描述或者例子中哪里不对望指正。
from interview.
Related Issues (20)
- CPP动态多态处勘误 HOT 3
- 单词里少了个字母 HOT 2
- 快排算法的疑问 HOT 3
- c++
- 快排里的else left++会执行吗?
- Interview HOT 3
- C++ 的inline 远不止如此 HOT 3
- #pragma pack(n) 应该强调是编译器扩展 HOT 2
- 使用typedef struct在C++毫无意义
- 如何定义一个只能在栈上生成对象的类 HOT 3
- `dynamic_cast` 的描述不够严谨 HOT 10
- 关于 `extern “C”` 的描述,是不严谨,乃至错误的。 HOT 2
- C/C++ 智能指针中,unique_ptr小节有错别字(粗体处) HOT 3
- 图挂了
- dynamic_cast,向下转型返回空指针才对
- 大佬,图片挂了
- SMTP的全称似乎错了
- 无名管道,应为单工通信,写的半双工 HOT 2
- End HOT 5
- 大佬,我基于你的这个文档做了一个小程序,可否贴在这里呢? HOT 4
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from interview.