🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: gnu.gcc.bug
Article #1568

Temporaries destroyed too soon

#1568
From: Juan Domingo Martin Gomez
Date: Fri, 18 Dec 1992 21:14
77 lines
2216 bytes
Here is the description of the bug.
Please reply by e-mail as I don't have access to news.

GCC VERSION:
    gcc version 2.3.2

LANGUAGE:
    C++

SAMPLE INPUT FILE:

-----------------------------------------------------------------------
#include <iostream.h>

class Foo
{
public:
    Foo();
    ~Foo();

    Foo &method();
};

Foo f1()
{
    return Foo();
}

Foo::Foo()
{
    cout << "Foo created" << endl;
}

Foo::~Foo()
{
    cout << "Foo destroyed" << endl;
}

Foo &Foo::method()
{
    cout << "Method called for Foo " << endl;
    return *this;
}

main()
{
    // f1() returns a temporary object. The member function
    // method() returns a reference to the same object.
    f1().method().method();
}
-----------------------------------------------------------------------


COMMAND LINE:
    gcc -o foo foo.cc

PLATFORM:
    Sun SparcStation IPX (sun4c) running SunOS 4.1.3

CONFIGURATION OF GCC:
    -sparc-sun-sunos4.1

DESCRIPTION:

If you compile and run the example you will observe that the temporary
object (Foo) returned by f1() is destroyed *before* the method is called
for second time in the statement. In some other tests I have done it
happens the same: temporaries are destroyed just after they are referenced
for the first time. However it is possible (and legal, I think) to reference
a temporary more than once in a single statement. I think destructors for
temporaries should be called after the statement has finished its execution.

-- 
Juan D. Martin                     Centro Nacional de Microelectronica
<juando@cnm.us.es>                                   Edificio C.I.C.A.
Phone: 34-5-4623811                             Av/ Reina Mercedes s/n
Fax:   34-5-4624506                               41012-SEVILLA, SPAIN
Technical Details
Message-ID: <9212181914.AA05066@sparc1.cnm.us.es>
Path: arc1-ca.pugleaf.net!sparky!uunet!cis.ohio-state.edu!cnm.US.ES!juando

Show Full Headers
Complete Article Headers
Newsgroups: gnu.gcc.bug
Path: arc1-ca.pugleaf.net!sparky!uunet!cis.ohio-state.edu!cnm.US.ES!juando
From: juando@cnm.US.ES (Juan Domingo Martin Gomez)
Subject: Temporaries destroyed too soon
Message-ID: <9212181914.AA05066@sparc1.cnm.us.es>
Sender: gnulists@ai.mit.edu
Organization: GNUs Not Usenet
Distribution: gnu
Date: Fri, 18 Dec 1992 21:14:45 GMT
Approved: bug-gcc@prep.ai.mit.edu
Lines: 79