当前位置:网站首页>本周小贴士#141:注意隐式转换到bool
本周小贴士#141:注意隐式转换到bool
2022-07-07 15:39:00 【-飞鹤-】
作为TotW#141最初发表于2018年1月19日
由Samuel Freilich创作
两种空指针检查
在解引用之前检查指针是否为空来避免崩溃和错误很重要。这可以通过两种方式完成:
if (foo) {
DoSomething(*foo);
}
if (foo != nullptr) {
DoSomething(*foo);
}
鉴于foo是一个指针,这两个条件具有相同的语义,但后面的类型检查更严格点。在C++中,许多类型都可以隐式转换为bool,当指向的类型本身转换为bool时需要额外注意。
考虑以下代码,它可能具有两个截然不同的含义:
bool* is_migrated = ...;
// 这是在检测is_migrated是否为空,或者真实意图是校验*is_migrated为真呢?
if (is_migrated) {
...
}
这份代码更清晰:
// 看起来像是对一个bool*的空指针进行检查
if (is_migrated != nullptr) {
...
}
这两种样式在 Google C++ 代码中都是可接受的。 所以当底层类型不能隐式转换为 bool 时,遵循周围代码的风格。 如果所讨论的值是像 std::unique_ptr 这样的“智能指针”,则语义和权衡是相同的。
可选值和作用域分布
可选(例如 absl::optional)值呢? 他们值得更仔细的考虑。
例如:
absl::optional<bool> b = MaybeBool();
if (b) {
... } // 当函数返回absl::optional(false)时发生了什么?
将变量声明放在 if 语句的条件中会限制变量的作用域,但该值会隐式转换为布尔值,因此可能无法明确测试哪个布尔属性。
以下代码的意图更清楚:
absl::optional<bool> b = MaybeBool();
if (b.has_value()) {
... }
请注意,实际上,上面的代码片段是等价的:absl::optional 到 bool 的转换只查看 optional 是否已满,而不是查看其内容。 读者可能会发现 optional(false) 为 true 有悖常理,但很明显 optional(false) 有一个值。 同样,当底层类型可以隐式转换为 bool 时,需要格外小心。
可选返回值的一种模式是将变量声明放在 if 语句的条件中。 这限制了变量的范围,但涉及到布尔的隐式转换:
if (absl::optional<Foo> foo = MaybeFoo()) {
DoSomething(*foo);
}
注意:在 C++17 中,if 语句可以包含初始化器,因此可以限制声明的范围,同时避免隐式转换:
if (absl::optional<Foo> foo = MaybeFoo(); foo.has_value()) {
DoSomething(*foo);
}
类布尔枚举
假设您已经采纳了技巧 #94 的建议,并决定在函数签名中使用枚举而不是 bool,以便在调用站点获得更好的可读性。 这种重构可能会在函数定义中引入隐式转换:
void ParseCommandLineFlags(
const char* usage, int* argc, char*** argv,
StripFlagsMode strip_flags_mode) {
if (strip_flags_mode) {
// 哪个值又是真呢?
...
}
}
您可以通过用显式比较替换隐式转换来获得额外的清晰度:
void ParseCommandLineFlags(
const char* usage, int* argc, char*** argv,
StripFlagsMode strip_flags_mode) {
if (strip_flags_mode == kPreserveFlags) {
...
}
}
总结
总之,注意隐匿转换为bool可能是不清楚的,因此考虑编写更显式的代码:
- 将指针类型与 nullptr 进行比较(特别是如果指向的类型可以隐式转换为 bool)。
- 使用诸如 absl::optional::has_value() 之类的布尔函数测试容器是否为空(特别是如果包含的类型可以隐式转换为 bool)。 对 if 使用可选的初始化形式来限制变量的范围(提示 #165)。 不过请记住只调用接口,不要获取 value() 或 has_value() 的地址。 testing::Optional 匹配器可以帮助测试。
- 将枚举与特定值进行比较。
更详细参考:Contextual conversions
边栏推荐
- L1-027 出租(Lua)
- 【Seaborn】组合图表、多子图的实现
- [source code interpretation] | source code interpretation of livelistenerbus
- On Apache Doris Fe processing query SQL source code analysis
- User defined view essential knowledge, Android R & D post must ask 30+ advanced interview questions
- Flask搭建api服务
- 服务器彻底坏了,无法修复,如何利用备份无损恢复成虚拟机?
- 99% of users often make mistakes in power Bi cloud reports
- 大笨钟(Lua)
- MySQL implements the query of merging two fields into one field
猜你喜欢
![[Seaborn] combination chart: facetgrid, jointgrid, pairgrid](/img/89/a7cf40fb3a7622cb78ea1b92ffd2fb.png)
[Seaborn] combination chart: facetgrid, jointgrid, pairgrid

自定义View必备知识,Android研发岗必问30+道高级面试题

Skimage learning (1)
![[image sensor] correlated double sampling CDs](/img/1c/3a641ad47ff91536db602dedc82705.png)
[image sensor] correlated double sampling CDs

Siggraph 2022 best technical paper award comes out! Chen Baoquan team of Peking University was nominated for honorary nomination

AI来搞财富分配比人更公平?来自DeepMind的多人博弈游戏研究

How to add aplayer music player in blog

赋能智慧电力建设 | 麒麟信安高可用集群管理系统,保障用户关键业务连续性

Seaborn data visualization

Leetcode brush questions day49
随机推荐
Rpcms method of obtaining articles under the specified classification
[Fantan] how to design a test platform?
数值 - number(Lua)
Solidity函数学习
麒麟信安操作系统衍生产品解决方案 | 存储多路径管理系统,有效提高数据传输可靠性
Reflections on "product managers must read: five classic innovative thinking models"
L1-019 谁先倒(Lua)
【网络攻防原理与技术】第6章:特洛伊木马
LeetCode 312. Poke balloon daily
Sator launched Web3 game "satorspace" and launched hoobi
Number of exchanges in the 9th Blue Bridge Cup finals
[fan Tan] after the arrival of Web3.0, where should testers go? (ten predictions and suggestions)
rpcms获取指定分类下的文章的方法
跟奥巴马一起画方块(Lua)
Flask搭建api服务-生成API文档
LeetCode 1477. Find two subarrays with sum as the target value and no overlap
skimage学习(3)——Gamma 和 log对比度调整、直方图均衡、为灰度图像着色
MRS离线数据分析:通过Flink作业处理OBS数据
LeetCode 1981. Minimize the difference between the target value and the selected element one question per day
赋能智慧电力建设 | 麒麟信安高可用集群管理系统,保障用户关键业务连续性