Perfect forwarding in C++.
Xiahua Liu August 28, 2024 #C++Perfect forwarding comes from std::forward()
is one of the special C++ technique to reduce the code length.
More information can be found on cppreference.com.
How it works
Basically it forwards a rvalue reference to the nested function inside a function template.
void
- If
T
is a lvalue reference,std::forward<T>()
does nothing,arg
will be passed as lvalue reference as normal. - If
T
is a rvalue reference, sincearg
is treated as an lvalue in the body of the function,std::forward<T>()
converts it back to rvalue reference.
For both cases, foo()
, which takes an rvalue parameter OR lvalue reference, will work correctly.
Difference from std::move()
The difference from std::move()
is that, std::forward<T>()
is conventionally used in function template only.
- For case 1 above,
std::forward<T>()
is no-op, it doesn't do anything. - For case 2 above,
std::forward<T>()
is same asstd::move()
here.
You can think std::forward<T>()
is a smarter version of std::move()
, however it requires a typename to work.