This is the mail archive of the gcc-bugs@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[Bug c++/17435] New: Temporay bound to reference is immediately destructed


If a reference to const is bound to an temporary object of derived type, it 
seems as if the lifetime of the temporary is not extended to the lifetime of 
the reference. 
 
The small program at the bottom, compiled with gcc version 4.0.0 20040912 
(experimental) gives this Output: 
 
~A() 
 A::func() 
 
With gcc 3.3.4 the same program generates this Output: 
 
 A::func() 
~A() 
 
I think this is the correct behaviour. 
 
 
To reproduce, save the following snippet to a file "name.cpp" and compile with 
gcc version 4.0.0 20040912: 
c++ -Wall -o name name.cpp 
Then execute "name". 
 
------------------------------------------------------ 
 
int printf(char const*, ...); 
 
struct A  
{ 
 
void func() const 
 { 
 printf(" A::func()\n"); 
 } 
 
~A() 
 { 
 printf("~A()\n"); 
 } 
 
}; 
 
struct B : public A  
{ 
 
}; 
 
int main() 
{ 
A const& r1 = B(); 
r1.func(); 
} 
 
----------------------------------------------------------

-- 
           Summary: Temporay bound to reference is immediately destructed
           Product: gcc
           Version: 4.0.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P2
         Component: c++
        AssignedTo: unassigned at gcc dot gnu dot org
        ReportedBy: vlukas at gmx dot de
                CC: gcc-bugs at gcc dot gnu dot org
 GCC build triplet: i686-pc-linux-gnu
  GCC host triplet: i686-pc-linux-gnu
GCC target triplet: i686-pc-linux-gnu


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=17435


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]