This is the mail archive of the
gcc-bugs@gcc.gnu.org
mailing list for the GCC project.
[Bug c++/14035] New: Compiler generates spurious temporary
- From: "austern at apple dot com" <gcc-bugzilla at gcc dot gnu dot org>
- To: gcc-bugs at gcc dot gnu dot org
- Date: 5 Feb 2004 23:12:34 -0000
- Subject: [Bug c++/14035] New: Compiler generates spurious temporary
- Reply-to: gcc-bugzilla at gcc dot gnu dot org
Consider the following test case:
#include <stdio.h>
struct Blob {
int x, y;
Blob() { }
Blob(const Blob &b) { printf("Copying %p to %p!!\n", &b, this); }
};
struct Blobby : public Blob { };
struct Wooly {
operator const Blobby & ()
{
printf("Conversion operator yields: %p\n", &myBlobby);
return myBlobby;
}
Blobby myBlobby;
};
void catcher(const Blob &blo)
{ printf("catcher: %p\n", &blo); }
int main()
{
Wooly wooly;
catcher((const Blob &)wooly); // <-- generates a copy
}
Compiling and running this gives the following output:
[tmp]$ ./a.out
Conversion operator yields: 0xbffffd50
Copying 0xbffffd50 to 0xbffffd60!!
catcher: 0xbffffd60
[tmp]$
This is wrong. The conversion operator yields a const Blobby&, and we're asking for a const Blob&. All
we need to do is perform the derived-to-base standard conversion. Instead, for no apparent reason,
the compiler invokes Blob's copy constructor and creates a temporary.
Note that this test case really does need the explicit cast of wooly to const Blob&. Without the explicit
cast we don't get a temporary.
--
Summary: Compiler generates spurious temporary
Product: gcc
Version: 3.5.0
Status: UNCONFIRMED
Severity: normal
Priority: P2
Component: c++
AssignedTo: unassigned at gcc dot gnu dot org
ReportedBy: austern at apple dot com
CC: gcc-bugs at gcc dot gnu dot org
GCC build triplet: powerpc-apple-darwin
GCC host triplet: powerpc-apple-darwin
GCC target triplet: powerpc-apple-darwin6.6
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=14035