Why is there a performance warning on cast pointer to bool?

前端 未结 6 1939
花落未央
花落未央 2020-12-09 01:14

Extends.

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

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

Even with a (bool) cast, the

6条回答
  •  遥遥无期
    2020-12-09 01:54

    The compiler needs to generate additional code for converting a pointer to bool. It is basically a comparision against zero and setting the result to one if not zero.

    00000000004005e0 <_Z4testPv>:
    bool test(void* adr) {
      4005e0:       48 85 ff                test   %rdi,%rdi
      4005e3:       0f 95 c0                setne  %al
        return adr;
    }
      4005f8:       c3                      retq
    

    This isn't directly visible from the source, so the compiler thinks this is something the user should be warned about.

提交回复
热议问题