C ++依赖于隐式转换为bool的条件? [英] C++ rely on implicit conversion to bool in conditions?

查看:97
本文介绍了C ++依赖于隐式转换为bool的条件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在编码标准表中找到了以下规则:

$ b $



if(ptr)//错误

p>

if(ptr!= NULL)// ok



div class =h2_lin>解决方案

在最严格的意义上,你可以依赖隐式转换bool。向后兼容C需要它。



因此,它成为一个代码可读性的问题。通常代码标准的目的是强制与代码风格相同,无论你是否同意风格。如果你正在考虑别人的标准,想知道你是否应该将它纳入自己的标准,请继续讨论它 - 但如果它是你公司的长期规则,学会与它一起生活。


I found the following rule in a coding standards sheet :

Do not rely on implicit conversion to bool in conditions.

if (ptr) // wrong

if (ptr != NULL) // ok

How reasonable/usefull is this rule?

How much overload on the compiled code?

解决方案

In the strictest sense, you can rely on implicit conversions to bool. Backwards compatibility with C demands it.

Thus it becomes a question of code readability. Often the purpose of code standards is to enforce a sameness to the code style, whether you agree with the style or not. If you're looking at someone else's standard and wondering if you should incorporate it into your own, go ahead and debate it - but if it's your company's long-standing rule, learn to live with it.

这篇关于C ++依赖于隐式转换为bool的条件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆