🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

1 total messages Started by Juan Domingo Martin Gomez Fri, 18 Dec 1992 21:14
Temporaries destroyed too soon
#1568
Author: 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
Thread Navigation

This is a paginated view of messages in the thread with full content displayed inline.

Messages are displayed in chronological order, with the original post highlighted in green.

Use pagination controls to navigate through all messages in large threads.

Back to All Threads