Move constructor on derived object

前端 未结 3 980
一个人的身影
一个人的身影 2020-11-29 05:38

When you have a derived object with a move constructor, and the base object also has move semantics, what is the proper way to call the base object move constructor from the

相关标签:
3条回答
  • 2020-11-29 06:09

    Named R-value references are treated as L-value.

    So we need std::move to convert it to R-Value.

    0 讨论(0)
  • 2020-11-29 06:20

    You really should use std::forward(obj) rather than std::move(obj). Forward will return the proper rvalue or lvalue based on the what obj is whereas move will turn an lvalue into an rvalue.

    0 讨论(0)
  • 2020-11-29 06:25

    rval is not a Rvalue. It is an Lvalue inside the body of the move constructor. That's why we have to explicitly invoke std::move.

    Refer this. The important note is

    Note above that the argument x is treated as an lvalue internal to the move functions, even though it is declared as an rvalue reference parameter. That's why it is necessary to say move(x) instead of just x when passing down to the base class. This is a key safety feature of move semantics designed to prevent accidently moving twice from some named variable. All moves occur only from rvalues, or with an explicit cast to rvalue such as using std::move. If you have a name for the variable, it is an lvalue.

    0 讨论(0)
提交回复
热议问题