Does binding temporary to a reference require a copy constructor in C++?

Posted by vitaut on Stack Overflow See other posts from Stack Overflow or by vitaut
Published on 2012-12-16T04:59:49Z Indexed on 2012/12/16 5:03 UTC
Read the original article Hit count: 154

Consider the following code:

class A {
  A(const A&);
 public:
  A() {}
};

int main() {
  const A &a = A();
}

This code compiles fine with GCC, but fails to compile with Visual C++ with the following error:

test.cc(8) : error C2248: 'A::A' : cannot access private member declared in class 'A'
        test.cc(2) : see declaration of 'A::A'
        test.cc(1) : see declaration of 'A'

So is it necessary to have a copy constructor accessible when binding a temporary to a reference?

© Stack Overflow or respective owner

Related posts about c++

Related posts about reference