编译器如何隐式地进行这种转换
How does the compiler makes this conversion implicitly?
我正在观看一个来自//build的视频,其中Herb Sutter用代码片段展示了显式转换关键字的好处:
template< /* ... */ > class unique_ptr {
public:
// ...
explicit operator bool() const { return get() != nullptr; }
他说通过这个关键字,我们可以防止编译
use(ptr * 42); // oops, meant *(ptr) * 42
我真的看不懂,这个展柜是怎么编译的?编译器如何进行转换?到什么类型?
它隐式地从unique_ptr
转换为bool,然后从bool转换为int来进行乘法。
(bool to int表示true为1,false为0)
相关文章:
- 防止主数据类型C++的隐式转换
- 模板参数替换失败,并且未完成隐式转换
- 复制列表初始化的隐式转换的等级是多少
- 不能将复制初始化与隐式转换的多个步骤一起使用
- 逐位操作的隐式类型转换
- 返回时不允许隐式转换
- 尝试使用算术运算符阻止隐式转换
- 获取隐式转换溢出从无符号到已签名的警告
- 启用从"vector<const T>&"到"const vector&"的隐式转换<T>
- C ++中无符号位长度类型之间的隐式转换,即uint8_t,uint16_t
- 隐式重新解释引用时强制转换,没有警告/错误
- 复制构造函数隐式转换问题
- 错误:隐式转换更改符号:'int'到'unsigned long'
- 将"uint8_t"(从套接字读取)隐式转换为"char"安全吗
- 如何检测 std::vector::emplace_back 上的隐式转换损失整数精度
- 如何防止双精度值到整数的隐式转换
- 如何解决隐式转换丢失整数精度:'size_t'(又名"无符号长")到'int'警告?
- 使用用户定义的转换操作员隐式转换
- 在比较中使用用户定义的转换和隐式转换
- C++显式转换和隐式转换