Can we return objects having a deleted/private copy/move constructor by value from a function?

前端 未结 5 2044
走了就别回头了
走了就别回头了 2020-11-29 08:36

In C++03 it is impossible to return an object of a class having a private non-defined copy constructor by value:

struct A { A(int x) { ... } private: A(A con         


        
5条回答
  •  暖寄归人
    2020-11-29 09:12

    The above code is still ill-formed in C++11. But you could add a public move constructor to A and then it would be legal:

    struct A
    {
        A(int x) {}
        A(A&&);
    private:
        A(A const&);
    };
    
    A f() {
      return A(10); // Ok!
    }
    

提交回复
热议问题