In some situation, we might want to preserve the value type of the arguments during #forwarding instead of toss away it after entering the local scope.
We could use the function std::forward
imported from <utility>
. std::forward
, like std::move
, move the value without copying. However, unlike std::move
, it moves the value conditionally, meaning it will preserve the value type of the passed value. If the value type of the passed value is lvalue, std::forward
will forward it as lvalue. If it is rvalue, std::forward
will forward it as rvalue.
In such way, we will not need to worry about the value type that passed to the function.