
Message du 05/05/11 23:50 De : "John Bytheway" A : boost@lists.boost.org Copie à : Objet : Re: [boost] std::map::find() wrapper
On 05/05/11 19:35, Marsh Ray wrote:
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(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? Best, Vicente