Copy Constructor Needed with temp object

前端 未结 2 1201
猫巷女王i
猫巷女王i 2020-12-16 02:00

The following code only works when the copy constructor is available.

When I add print statements (via std::cout) and make the copy constructor availa

2条回答
  •  长情又很酷
    2020-12-16 02:37

    From http://gcc.gnu.org/gcc-3.4/changes.html

    When binding an rvalue of class type to a reference, the copy constructor of the class must be accessible. For instance, consider the following code:

    class A 
    {
    public:
      A();
    
    private:
      A(const A&);   // private copy ctor
    };
    
    A makeA(void);
    void foo(const A&);
    
    void bar(void)
    {
      foo(A());       // error, copy ctor is not accessible
      foo(makeA());   // error, copy ctor is not accessible
    
      A a1;
      foo(a1);        // OK, a1 is a lvalue
    }
    

    This might be surprising at first sight, especially since most popular compilers do not correctly implement this rule (further details).

    This will be fixed in C++1x by Core Issue 391.

提交回复
热议问题