g++ bug? Seeking C++ language lawyer help (dcl.init.ref)

Turly O'Connor turly@apple.com
Mon Feb 25 13:15:00 GMT 2002


Hello there,

Hope all is well.

We have some code here which (ab)uses references, see the little example 
program below. g++3 creates a temporary object at the commented line and 
this, contends the author, is wrong.  (gcc2.95.2 did not create the 
temporary.) From my reading of the standard's dcl.init.ref, I agree and 
don't think the temporary is required, because the 'operator &'  means 
that dcl.init.ref/6 applies and so direct binding should take place.  
But IANAL, so I would appreciate further input.  Is gcc3 justified in 
creating a temporary, or is this a bug?

[Code in cp/call.c (convert_like_real (), case USER_CONV) always calls 
the constructor if TOTYPE is an aggregate without regard for whether 
CONVFN was an reference 'operator &'.]

Thanks muchly for any input you can provide, and, as always...

Have Fun!

--turly
--
So, a three legged dog walks into a bar and says, 'I'm looking for the 
guy that shot my paw.'

--------------------------------------  
SNIP --------------------------------------

#include <cstdio>

struct Blob {
     int x, y;
     Blob() { }
     Blob(const Blob &b) { printf("!!Copying %p to %p!! ", &b, this); }
};
struct Blobby : public Blob { };

struct Wooly {
     operator Blobby & ()  {
         printf("Yielding myBlobby@%p ", &myBlobby);
         return myBlobby;
     }
     Blobby myBlobby;
};

Blobby &fooey()
{
     static Blobby source;
     printf("Yielding Blobby@%p ", &source);
     return source;
}

void catcher(const Blob &blo)
{ printf("blo@%p\n", &blo); }

int main()
{
     Wooly wooly;
     catcher(wooly);
     catcher((Blob &)fooey());
     catcher((Blob &)wooly);    // <-- generates a copy
     return 0;
}



More information about the Gcc-bugs mailing list