The program below compiles and behaves as expected with gcc 3.3.1(*), but not
with 3.4.2. I believe that it is the older version of the compiler that is 
right.

gcc 3.4.2 will not compile the program as long as the copy constructor for the
"x" class is private, even though the copy constructor should never be called
(outside function "make_x", which is a friend of "x").

If the copy constructor is made public, the program compiles with gcc 3.4.2 and
gives the same result as it did with gcc 3.3.1.

It appears that the copy constructor is in fact never called at all, not even
inside the function "make_x", but I assume that avoiding the copy constructor
when returning the function value in "make_x" is a legal optimization made by
the compiler.

---------
(*) I actually made one further simplification of the example after I tried it
the last time under 3.3.1, and I since I no longer have convenient access to
that version of gcc I have been unable to rerun the test with the old compiler.
But I still belive that the first sentence is a true statement.



The program:
-------------------------------------------------------------------------------
#include <iostream>

class x
{
public:
  x() {std::cout << "x() default constructor" << std::endl;}

private:
  x(const x& c) {std::cout << "x(x) copy constructor" << std::endl;}
  friend const x make_x();
};

const x make_x() {return x();}

inline void func(const x& p) {}

int main (int argc, char* argv[])
{
  // The next line compiles with gcc 3.3.1, but not with 3.4.2,
  // when x's copy constructor is private.
  func(make_x());

  return 0;
}


Error message from gcc 3.4.2:
-------------------------------------------------------------------------------
e53_gcc.cpp: In function `int main(int, char**)':
e53_gcc.cpp:9: error: `x::x(const x&)' is private
e53_gcc.cpp:21: error: within this context


Version number from gcc 3.4.2:
-------------------------------------------------------------------------------
gcc (GCC) 3.4.2 (mingw-special)
Copyright (C) 2004 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.


Version number from gcc 3.3.1 (the old version that works as expected):
-------------------------------------------------------------------------------
gcc (GCC) 3.3.1 (mingw special 20030804-1)
Copyright (C) 2003 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.

-- 
           Summary: Unused copy constructor can't be private
           Product: gcc
           Version: 3.4.2
            Status: UNCONFIRMED
          Severity: normal
          Priority: P2
         Component: c++
        AssignedTo: unassigned at gcc dot gnu dot org
        ReportedBy: christian dot engstrom at glindra dot org
                CC: gcc-bugs at gcc dot gnu dot org


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

Reply via email to