C++11 move constructor

前端 未结 4 1947
清酒与你
清酒与你 2020-12-08 01:14


What would be the correct way to implement a move constructor considering the following class:

class C {
public:
    C();
    C(C&& c);
private         


        
4条回答
  •  隐瞒了意图╮
    2020-12-08 01:59

    Both Constructors should work. So both are correct move constructors. The second one might be more efficient, since the first one default constructs string only to assign to it, while the second will simply move construct it and should therefore be more efficient. If the second one is less efficient I would suspect a compiler bug (remember that C++11 support is still not complete for current compilers) or a flawed test methology (how exactly do you test copy vs move and are you sure the move constructor not the assignment op is called in both cases?).

    Of course whenever possibly you could simply let the compiler generate your constructor via C(C&&) = default;.

提交回复
热议问题