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]

Re: G++: Non-const copy ctor sometimes ignored


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

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