Perfect Forwarding

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.

Links to this page
  • Forwarding References

    Although forwarding references is a good alternative for generality, it does not actually preserve the value type of the arguments after they enter the local scope. In order to do this, we need 202203291851#.

#cpp