[EMCpp]Item-25 Use std::move on Rvalue References, std::forward on Universal References
· β 4 min read
But don’t apply std::move
or std::forward
to local objects if they would otherwise be eligible for the return value optimization.
But don’t apply std::move
or std::forward
to local objects if they would otherwise be eligible for the return value optimization.
If a function template parameter has type T&&
for a deduced type T
, or if an object is declared using auto&&
, the parameter or object is a universal reference.
std::move
performs an unconditional cast to an rvalue, while std::forward
casts its argument to an rvalue only if that argument is bound to an rvalue.
For std::unique_ptr
pImpl pointers, declare special member functions in the class header, but implement them in the implementation file.
Compared to new
, make functions eliminate source code duplication, improve exception safety, and, for std::make_shard
and std::allocate_shared
, generate code that’s smaller and faster.