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] Temporay bound to reference is immediately destructed


------- Additional Comments From gdr at integrable-solutions dot net  2004-09-12 23:49 -------
Subject: Re:  Temporay bound to reference is immediately destructed

"bangerth at dealii dot org" <gcc-bugzilla@gcc.gnu.org> writes:

| This example makes it slightly clearer what happens: 
| ------------------ 
| #include <iostream> 
| #include <typeinfo> 
|  
| struct A { 
|     virtual ~A() {}; 
| };  
|   
| struct B : public A  { };  
|   
| int main() {  
|   A const& r1 = B();  
|   std::cout << typeid(r1).name() << std::endl; 
| }  
| ------------------------ 
|  
| g/x> /home/bangerth/bin/gcc-3.5-pre/bin/c++ x.cc 
| g/x> ./a.out  
| 1A 
| g/x> /home/bangerth/bin/gcc-3.3.4-pre/bin/c++ x.cc 
| g/x> ./a.out  
| 1B 
| g/x> icc -Xc -ansi -c x.cc 
| g/x> ./a.out  
| 1B 
|  
| In contrast to older versions of gcc and to icc, gcc chooses to copy the 
| A part of the object created by B() when it assigns to the reference. 
|  
| I believe that this is standard conforming, and that gcc is allowed (though 
| not required) to do so, but someone else will have to look up the respective 
| sentence of the standard. 

I believe this is a bug in GCC, according to 8.5.3/5, third
alternative in the first bullet:

  A reference to type "cv1 T1" is initialized by an expression of type
  "cv2 T2"as follows:
  -- If the initializer expression
     [...]
     -- If the initializer expression is an rvalue, with T2 a class
        type, and "cv1 T1" is reference-compatible with "cv2 T2",
        the reference is bound in one of the following ways (the
        choice is implementation-defined):
        -- The reference is bound to the object represented by the
           rvalue (see 3.10) or to a sub-object within that object.

        -- A temporary of type "cv1 T2" [sic] is created, and a
           constructor is called to copy the entire rvalue object into
           the temporary. The reference is bound to the temporary or
           to a sub-object within the temporary.93)


[that part of the standard is a clear abuse of switch-statements :-)]

The bottom line is that even if a copy occurs, the whole object is
occupied, not just the "T1" subobject.  This new GCC behaviour is just
at odd with C++ semantics from the dark ages.

-- Gaby


-- 


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]