G++: Non-const copy ctor sometimes ignored

Gabriel Dos Reis Gabriel.Dos-Reis@cmla.ens-cachan.fr
Sun Feb 27 14:57:00 GMT 2000


Chip Salzenberg <chip@valinux.com> writes:

| 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, AFAICT B::B(const A&) should be used to initialize the returned
value. So g++ should accept the code.  So this is a bug in the
front-end. 

| ----------------------------------------------------------------
| 
| 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 &)

-- Gaby


More information about the Gcc-bugs mailing list