public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/16982] New: wrong copy ctor called
@ 2004-08-11 15:42 boris at kolpackov dot net
  2004-08-11 16:21 ` [Bug c++/16982] " pinskia at gcc dot gnu dot org
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: boris at kolpackov dot net @ 2004-08-11 15:42 UTC (permalink / raw)
  To: gcc-bugs

$ cat >test.cxx
extern "C" void
abort ();

struct base
{
  base ()
  {
  }
  
  base (base const&)
  {
    abort ();
  }
};

struct derived : base
{
  derived ()
  {
  }
  
  derived (derived const&)
      : base ()
  {
  }
};


struct s
{
  operator derived ()
  {
    return derived ();
  }
};

int
main ()
{
  s s;

  base const& b (s);
}

$ g++-3.4 --version
g++-3.4 (GCC) 3.4.1 (Debian 3.4.1-5)
$ g++-3.4 test.cxx
$ ./a.out
Aborted

$ g++-3.3 --version
g++-3.3 (GCC) 3.3.4 (Debian 1:3.3.4-7)
$ g++-3.3 test.cxx
$ ./a.out
Aborted

Here is a quote from 8.5.3/5 bullet 2, sub-bullet 1, sub-sub-bullet 2:

  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 subobject within the temporary. 

  The constructor that would be used to make the copy shall be callable whether
or not the copy is actually done. 

  [Example: 

   struct A { }; 
   struct B : public A { } b; 
   extern B f();
   const A& rca = f(); // Either bound to the A subobject of the B rvalue,
                       // or the entire B object is copied and the reference
                       // is bound to the A subobject of the copy
  --end example]

-- 
           Summary: wrong copy ctor called
           Product: gcc
           Version: 3.4.1
            Status: UNCONFIRMED
          Severity: normal
          Priority: P2
         Component: c++
        AssignedTo: unassigned at gcc dot gnu dot org
        ReportedBy: boris at kolpackov dot net
                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=16982


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

* [Bug c++/16982] wrong copy ctor called
  2004-08-11 15:42 [Bug c++/16982] New: wrong copy ctor called boris at kolpackov dot net
@ 2004-08-11 16:21 ` pinskia at gcc dot gnu dot org
  2004-08-11 16:31 ` boris at kolpackov dot net
  2004-08-11 18:12 ` bangerth at dealii dot org
  2 siblings, 0 replies; 4+ messages in thread
From: pinskia at gcc dot gnu dot org @ 2004-08-11 16:21 UTC (permalink / raw)
  To: gcc-bugs


------- Additional Comments From pinskia at gcc dot gnu dot org  2004-08-11 16:21 -------
I think this is related to bug 14140.

-- 


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


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

* [Bug c++/16982] wrong copy ctor called
  2004-08-11 15:42 [Bug c++/16982] New: wrong copy ctor called boris at kolpackov dot net
  2004-08-11 16:21 ` [Bug c++/16982] " pinskia at gcc dot gnu dot org
@ 2004-08-11 16:31 ` boris at kolpackov dot net
  2004-08-11 18:12 ` bangerth at dealii dot org
  2 siblings, 0 replies; 4+ messages in thread
From: boris at kolpackov dot net @ 2004-08-11 16:31 UTC (permalink / raw)
  To: gcc-bugs


------- Additional Comments From boris at kolpackov dot net  2004-08-11 16:31 -------
In 14140 we are talking about slice-copying (note the signature: void func(base
b)). Here we are talking about reference binding. Those two cases may look alike
on the source code level but they are covered by completely different parts of
the standard.

-- 


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


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

* [Bug c++/16982] wrong copy ctor called
  2004-08-11 15:42 [Bug c++/16982] New: wrong copy ctor called boris at kolpackov dot net
  2004-08-11 16:21 ` [Bug c++/16982] " pinskia at gcc dot gnu dot org
  2004-08-11 16:31 ` boris at kolpackov dot net
@ 2004-08-11 18:12 ` bangerth at dealii dot org
  2 siblings, 0 replies; 4+ messages in thread
From: bangerth at dealii dot org @ 2004-08-11 18:12 UTC (permalink / raw)
  To: gcc-bugs


------- Additional Comments From bangerth at dealii dot org  2004-08-11 18:12 -------
Here's a testcase that's slightly simpler to understand: 
-------------------- 
#include <iostream> 
extern "C" void abort (); 
 
#define WHEREAMI std::cout << __PRETTY_FUNCTION__ << std::endl 
 
struct base { 
  base () {WHEREAMI;} 
   
  base (base const&) { 
    WHEREAMI; 
    abort (); 
  } 
}; 
 
struct derived : base { 
  derived () { WHEREAMI; } 
  derived (derived const&) : base () { WHEREAMI; } 
}; 
 
 
struct s { 
  operator derived () { 
    WHEREAMI; 
    return derived (); 
  } 
}; 
 
int main () { 
  s s; 
  base const& b (s); 
} 
------------------------------- 
 
With 3.4, we get this output: 
 
g/x> ./a.out  
s::operator derived() 
base::base() 
derived::derived() 
base::base(const base&) 
Aborted 
 
In other words, the abort happens when the temporary returned from 
the call to s::operator derived is casted to a reference of the base 
class. I am pretty sure the compiler is allowed to do so, but don't 
right now have the correct section of the standard available. 
 
In any case, the question may be moot, since with mainline we get this: 
 
g/x> /home/bangerth/bin/gcc-3.5-pre/bin/c++ x.cc 
g/x> ./a.out  
s::operator derived() 
base::base() 
derived::derived() 
 
I.e. there is no abort and the program does what you expect. 
 
W. 

-- 
           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|UNCONFIRMED                 |RESOLVED
         Resolution|                            |FIXED
   Target Milestone|---                         |3.5.0


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


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

end of thread, other threads:[~2004-08-11 18:12 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2004-08-11 15:42 [Bug c++/16982] New: wrong copy ctor called boris at kolpackov dot net
2004-08-11 16:21 ` [Bug c++/16982] " pinskia at gcc dot gnu dot org
2004-08-11 16:31 ` boris at kolpackov dot net
2004-08-11 18:12 ` bangerth at dealii 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).