Pass by value vs pass by rvalue reference

前端 未结 7 945
独厮守ぢ
独厮守ぢ 2020-11-29 01:40

When should I declare my function as:

void foo(Widget w);

as opposed to

void foo(Widget&& w);?

Assume this

7条回答
  •  有刺的猬
    2020-11-29 01:44

    Choosing between by-value and by-rvalue-ref, with no other overloads, is not meaningful.

    With pass by value the actual argument can be an lvalue expression.

    With pass by rvalue-ref the actual argument must be an rvalue.


    If the function is storing a copy of the argument, then a sensible choice is between pass-by-value, and a set of overloads with pass-by-ref-to-const and pass-by-rvalue-ref. For an rvalue expression as actual argument the set of overloads can avoid one move. It's an engineering gut-feeling decision whether the micro-optimization is worth the added complexity and typing.

提交回复
热议问题