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]
Other format: [Raw text]

[Bug c++/15284] New: Access check on not-used copy-constructor


Setting a copy constructor private is a well-known
way to make sure that it never is copied.  This usually
works fine - except in this case:

#include <iostream>

class A {
#ifdef COMPILEBUG
private:
#else
public:
#endif
  A(A const&)
  {
    std::cout << "Copy constructor is actually being used (called)!" << std::endl;
  }
public:
  A(int)
  {
    std::cout << "Calling A(int)" << std::endl;
  }
};

A f()
{
  return A(1);
}

int main()
{
  A a = f();
}


If you compile this without defining COMPILEBUG - then
it compiles fine of course.  And the output shows that
the copy constructor of A is never called!

>g++-cvs-3.5 test.cc
>a.out
Calling A(int)

Because the copy constructor is never called - one would
expect that it is ok to make it private; however:

>g++-cvs-3.5 -DCOMPILEBUG test.cc
test.cc: In function `A f()':
test.cc:10: error: `A::A(constA&)' is private
test.cc:22: error: within this context
test.cc:10: error: `A::A(constA&)' is private
test.cc:22: error: within this context
test.cc: In function `int main()':
test.cc:10: error: `A::A(constA&)' is private
test.cc:27: error: within this context
test.cc:10: error: `A::A(constA&)' is private
test.cc:27: error: within this context

The same behaviour is seen with all versions down to 2.95.2.

-- 
           Summary: Access check on not-used copy-constructor
           Product: gcc
           Version: 3.5.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P2
         Component: c++
        AssignedTo: unassigned at gcc dot gnu dot org
        ReportedBy: carlo at gcc dot gnu dot org
                CC: gcc-bugs at gcc dot gnu dot org


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


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