为什么有一个性能警告投射指针到bool? [英] Why is there a performance warning on cast pointer to bool?

查看:118
本文介绍了为什么有一个性能警告投射指针到bool?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

延伸

我想我做的很酷,当我做了像:

I thought I was being cool when I did something like:


bool hasParent()
{
  return this->parentNode ;
}

即使使用(bool)强制转换,警告仍然不会消失。

Even with a (bool) cast, the warning still doesn't go away.

其中this-> parentNode在没有父节点时为NULL。

Where this->parentNode is NULL when there is no parent node.

但我得到:


warning C4800: 'Node *' : forcing value to bool 'true' or 'false' (performance warning)

什么是交易,哟?为什么会出现性能警告?我认为不写下这样会更有效:

What's the deal, yo? Why is that a performance warning? I thought it'd be more efficient to not write something like:



bool hasParent()
{
  if( this->parentNode )
    return true ;
  else
    return false ;
}

但是第二个版本没有生成警告,编译器看起来更开心。

But the second version generates no warnings and the compiler seems a lot happier. Which is faster though?

推荐答案

有关于Microsoft Connect的讨论()。这是一个很重要的问题。给予Microsoft的示例是:

There's a discussion on Microsoft Connect about this (What is the performance implication of converting to bool in C++?). The example given to Microsoft is:

$ cat -n t.cpp && cl -c -W3 -O2 -nologo -Fa t.cpp
1 bool f1 (int i)
2 {
3 return i & 2;
4 }
5
6 bool f2 (int i)
7 {
8 const bool b = i & 2;
9 return b;
10 }
11
12 bool f3 (int i)
13 {
14 const bool b = 0 != (i & 2);
15 return b;
16 }
t.cpp
t.cpp(3) : warning C4800: 'int' : forcing value to bool 'true' or 'false' (performance warning)
t.cpp(8) : warning C4800: 'int' : forcing value to bool 'true' or 'false' (performance warning)

Microsoft的回应(负责警告的开发人员)是:

And Microsoft's response (from the developer responsible for the warning) is:


这个警告非常有用,并在昨天在我的代码中发现了一个错误。我认为马丁在上下文中采取了性能警告。

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

这不是关于生成的代码,而是关于程序员是否发出意图更改值从int到bool。有一个惩罚,并且用户可以选择使用int而不是bool一致(或更有可能反之亦然),以避免boolifyingcodegen。在下面的第三种情况下,警告被抑制,因为他明确表示他打算接受int-> bool转换。

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. The warning is suppressed in the third case below because he's clearly signalled his intent to accept the int->bool transition.

这是一个旧的警告,

因此,基本上MS开发者似乎在说,如果你想转换一个 int 到 bool 你应该更好地使用 return this-& = 0 ,而不是隐式或显式转换。

So basically the MS developer seems to be saying that if you want to 'cast' an int to bool you should more properly do it by using "return this->parentNode != 0" instead of an implicit or explicit cast.

我个人感兴趣的是更多地了解什么样的错误警告揭露。我认为这个警告不会有很大的价值。

Personally, I'd be interested to know more about what kind of bugs the warning uncovers. I'd think that this warning wouldn't have a whole lot of value.

这篇关于为什么有一个性能警告投射指针到bool?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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