This is the mail archive of the
gcc-bugs@gcc.gnu.org
mailing list for the GCC project.
g++ bug? Seeking C++ language lawyer help (dcl.init.ref)
- From: "Turly O'Connor" <turly at apple dot com>
- To: gcc-bugs at gcc dot gnu dot org
- Date: Mon, 25 Feb 2002 10:17:36 -0800
- Subject: g++ bug? Seeking C++ language lawyer help (dcl.init.ref)
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;
}