public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/14035] New: Compiler generates spurious temporary
@ 2004-02-05 23:12 austern at apple dot com
  2004-02-14 20:11 ` [Bug c++/14035] " pinskia at gcc dot gnu dot org
                   ` (5 more replies)
  0 siblings, 6 replies; 7+ messages in thread
From: austern at apple dot com @ 2004-02-05 23:12 UTC (permalink / raw)
  To: gcc-bugs

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


^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2004-10-19 23:37 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2004-02-05 23:12 [Bug c++/14035] New: Compiler generates spurious temporary austern at apple dot com
2004-02-14 20:11 ` [Bug c++/14035] " pinskia at gcc dot gnu dot org
2004-10-13 14:59 ` [Bug c++/14035] [3.3/3.4/4.0 Regression] " pinskia at gcc dot gnu dot org
2004-10-13 14:59 ` [Bug c++/14035] " pinskia at gcc dot gnu dot org
2004-10-15 21:27 ` [Bug c++/14035] [3.3/3.4/4.0 Regression] " mmitchel at gcc dot gnu dot org
2004-10-19 23:30 ` mmitchel at gcc dot gnu dot org
2004-10-19 23:37 ` pinskia at gcc dot gnu dot org

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).