This is the mail archive of the gcc@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]

Can't pass temporary with hidden copy ctor as const ref


Hi,

I'm having trouble compiling the following with g++ 4.2.1:

  class Uncopyable
  {
  public:
      Uncopyable(int x) {}
  private:
      Uncopyable(const Uncopyable & other) {}
  };

  class User
  {
  public:
      void foo(int x)
      {
          foo(Uncopyable(x));
      }

      void foo(const Uncopyable & x)
      {
          // do something
      }
  };

  int main ()
  {
      User u;
      u.foo(1);
      return 0;
  }

The compiler complains that it can't find a copy ctor for
'Noncopyable'; why is this? It would seem that temporaries can be
passed directly as the const ref rather than needing a copy.

Message:

test.cc: In member function 'void User::foo(int)':
test.cc:11: error: 'Uncopyable::Uncopyable(const Uncopyable&)' is private


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