|
Boost : |
Subject: Re: [boost] std::map::find() wrapper
From: John Bytheway (jbytheway+boost_at_[hidden])
Date: 2011-05-05 18:06:16
On 05/05/11 22:55, Vicente BOTET wrote:
>> Message du 05/05/11 23:50
>> De : "John Bytheway"
>> A : boost_at_[hidden]
>> Copie à :
>> Objet : Re: [boost] std::map::find() wrapper
>>
<snip>
>> There's this:
>>
>> struct A { int x; };
>> struct B { int y; };
>> struct C : A, B {};
>>
>> C& f(B& b) {
>> return static_cast(b);
>> }
>>
>> C* g(B* b) {
>> return static_cast(b);
>> }
>>
>> which compiles to:
>>
>> 0000000000000000 <_Z1fR1B>:
>> 0: 48 8d 47 fc lea -0x4(%rdi),%rax
>> 4: c3 retq
>>
>> 0000000000000010 <_Z1gP1B>:
>> 10: 48 8d 57 fc lea -0x4(%rdi),%rdx
>> 14: 31 c0 xor %eax,%eax
>> 16: 48 85 ff test %rdi,%rdi
>> 19: 48 0f 45 c2 cmovne %rdx,%rax
>> 1d: c3 retq
>>
>> The compiler doesn't have to do a null-check for the reference case, so
>> it's shorter.
>
> I would be interested in knowing why the compiler need to check for 0 when you apply a static_cast?
Because static_cast<C*>(b) is valid when b == 0, and must return 0, but
any non-zero value must be changed (by subtracting 4) because of the
multiple inheritance. This is a pretty obscure corner case, but it does
demonstrate that the non-NULL guarantee for references has at least some
performance implications.
John Bytheway
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk