Boost logo

Boost :

Subject: Re: [boost] std::map::find() wrapper
From: John Bytheway (jbytheway+boost_at_[hidden])
Date: 2011-05-05 17:48:33


On 05/05/11 19:35, Marsh Ray wrote:
<snip>
> So I'm interpreting that as "Can you give an example of an actual C++
> program where the compiler does not emit the same code for references as
> the analogous code using plain pointers."
>
> .... I played around with g++-4.6.0 and was not able to make it emit
> different code in the short time I'm willing to try. G++ seems to miss
> some opportunities for optimization of references, and seems to be smart
> about tracking pointer values.

There's this:

struct A { int x; };
struct B { int y; };
struct C : A, B {};

C& f(B& b) {
  return static_cast<C&>(b);
}

C* g(B* b) {
  return static_cast<C*>(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.

John Bytheway


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk