[EMCpp]Item-26 Avoid Overloading on Universal References
· β 4 min read
Overloading on universal references almost always leads to the universal reference overload being called more frequently than expected.
Overloading on universal references almost always leads to the universal reference overload being called more frequently than expected.
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.