How to enforce copy elision, why it won't work with deleted copy constructor?

前端 未结 3 402
孤独总比滥情好
孤独总比滥情好 2020-12-01 17:58

I have an uncopiable class. Copying this would be problematic. I want to guarantee that it won\'t be ever copied, so I made its copy constructor deleted

3条回答
  •  一整个雨季
    2020-12-01 18:27

    You can't force copy elision (yet) (see other answers).

    However, you can provide a default move constructor for your class, this will move (and thus, not copy) the return value if RVO/NRVO is not possible. To do this you should add = default for your move constructors:

    class A {
      public:
        A() = default;
        A(const A&) = delete;
        A(A&&) = default;
        A& operator=(A&&) = default;
    };
    

    Example

提交回复
热议问题