The bug is triggered by a very simple code below (it doesn't need anything to
include, etc)

---------------------------------
class A { };
class C : public A { 
public:
    C() {}
private:
    C(const C& ) : A() {} // copying prohibited
};
void f(const A& ref) {}
void g() {
    f(C());
}
---------------------------------
The compiler prints the following error messasge:

test_bug.cpp: In function 'void g()':
test_bug.cpp:6: error: 'C::C(const C&)' is private
test_bug.cpp:10: error: within this context

As easy to see, there is no need for a copy constructor here, because C is the
immediate descender of the class A, so it can be (and must be) cast to A&
without creating any temporaries.

BTW, if we replace the body of g() with smth. like this:

void g() {
    C c;
    f(c);
}

no errors are reported, and the compilation succeeds.

I tried this with 3.4 and 4.1, they do the same wrong thing. Funny to say, 2.96
compiles the code with no complains.

The most recent compiler I've got gives the following version info:

g++41 (GCC) 4.1.0 20050924 (experimental)
Copyright (C) 2005 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

As my 3.4 does the same, I guess that the bug remains uncaught for 1 year at
least  so I don't think it's already known. 

As far as I understand, the bug is serious because in fact compiler sil;ently
creates copies which it must not create; the only thing which let me know the
bug is that I've made the copy conastructor private to prohibit copying of some
complicated objects.


-- 
           Summary: Wrong attempts to create a copy of an anonymous object
           Product: gcc
           Version: 4.1.0
            Status: UNCONFIRMED
          Severity: major
          Priority: P3
         Component: c++
        AssignedTo: unassigned at gcc dot gnu dot org
        ReportedBy: z81 at backpath dot croco dot net


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

Reply via email to