在C ++中转换为bool的性能含义是什么? [英] What is the performance implication of converting to bool in C++?

查看:135
本文介绍了在C ++中转换为bool的性能含义是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

[此问题与 this one 。]



我的编译器警告隐式转换或转换某些类型为bool而显式转换不产生a warning:

  long t = 0; 
bool b = false;
b = t; // performance warning:forcing long to bool
b =(bool)t; // performance warning
b = bool(t); // performance warning
b = static_cast< bool>(t); // performance warning
b = t?真假; // ok,no warning
b = t!= 0; // ok
b = !! t; // ok

这是用Visual C ++ 2008,但我怀疑其他编译器可能有类似的警告。所以我的问题是:转换/转换为 bool 的性能含义是什么?显式转换在某些情况下(例如,对于某些目标架构或处理器)有更好的性能吗?隐式转换是否会混淆优化程序?



Microsoft的解释他们的警告并不是特别有帮助。

解决方案

我对这种行为感到困惑,直到我有一个很好的理由,找到此链接:



http:/ /connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=99633



显然,来自拥有此警告的Microsoft开发人员:


这个警告令人惊讶的是
有用,昨天在我的代码
中发现一个错误。我认为马丁是

上下文中取性能警告。



这不是关于生成的代码,
它是关于
程序员是否已通知意图到
将值从int更改为bool

有一个惩罚,并且
用户可以选择使用int
而不是bool一致(或
更有可能反之亦然),以避免
boolifyingcodegen。 [...]



这是一个旧警告,可能有
超过其目的,但
表现如此。


所以在我看来,警告是更多的风格,避免一些错误, 。



希望这将回答您的问题...



: - p


[This question is related to but not the same as this one.]

My compiler warns about implicitly converting or casting certain types to bool whereas explicit conversions do not produce a warning:

long t = 0;
bool b = false;
b = t;                    // performance warning: forcing long to bool
b = (bool)t;              // performance warning
b = bool(t);              // performance warning
b = static_cast<bool>(t); // performance warning
b = t ? true : false;     // ok, no warning
b = t != 0;               // ok
b = !!t;                  // ok

This is with Visual C++ 2008 but I suspect other compilers may have similar warnings.

So my question is: what is the performance implication of casting/converting to bool? Does explicit conversion have better performance in some circumstance (e.g., for certain target architectures or processors)? Does implicit conversion somehow confuse the optimizer?

Microsoft's explanation of their warning is not particularly helpful. They imply that there is a good reason but they don't explain it.

解决方案

I was puzzled by this behaviour, until I found this link:

http://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=99633

Apparently, coming from the Microsoft Developer who "owns" this warning:

This warning is surprisingly helpful, and found a bug in my code just yesterday. I think Martin is taking "performance warning" out of context.

It's not about the generated code, it's about whether or not the programmer has signalled an intent to change a value from int to bool. There is a penalty for that, and the user has the choice to use "int" instead of "bool" consistently (or more likely vice versa) to avoid the "boolifying" codegen. [...]

It is an old warning, and may have outlived its purpose, but it's behaving as designed here.

So it seems to me the warning is more about style and avoiding some mistakes than anything else.

Hope this will answer your question...

:-p

这篇关于在C ++中转换为bool的性能含义是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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