G++: Non-const copy ctor sometimes ignored

Chip Salzenberg chip@valinux.com
Sun Feb 27 14:44:00 GMT 2000


IIRC, the standard says that copy constructors may accept either a
ref or a const ref, and that a compiler shouldn't unnecessarily
discriminate against a non-const copy ctor.  If that's true, then the
following code should compile.  But as of 2/24 (the last snapshot that
bootstrapped for me), it doesn't:

----------------------------------------------------------------
class B;
class A {
  public:
    A()		 {}
    A(const A &) {}
    A(B &)	 {}
};
class B {
  public:
    B()		 {}
    B(B &)	 {}
    B(const A &) {}

    // Uncomment the following line and the error goes away:
    //B(const B &) {}
};

B func() { return A(); }
----------------------------------------------------------------

Here's the error -- and a mighty strange one it is, too:

copy.cc: In function `B func ()':
copy.cc:18: no matching function for call to `B::B (B)'
copy.cc:11: candidates are: B::B (B &)
copy.cc:12:                 B::B (const A &)

Help?
-- 
Chip Salzenberg              - a.k.a. -              <chip@valinux.com>
"I wanted to play hopscotch with the impenetrable mystery of existence,
    but he stepped in a wormhole and had to go in early."  // MST3K


More information about the Gcc-bugs mailing list