Perfect Forwarding & Type Traits
The forwarding problem
A wrapper should pass arguments to another function exactly as received: lvalues as lvalues (copy), rvalues as rvalues (move).
Forwarding references T&&
In a template with deduced T, T&& is a forwarding (universal) reference — it binds to anything:
template <typename T>
void wrapper(T&& arg) {
target(std::forward<T>(arg)); // preserves lvalue/rvalue-ness
}
- Pass an lvalue →
T=X&,argisX& - Pass an rvalue →
T=X,argisX&&(Reference collapsing:& &&→&,&& &&→&&.)
⚠️ std::string&& (no template deduction) is just an rvalue reference, not a forwarding reference.
std::move vs std::forward
| Use with | Effect | |
|---|---|---|
std::move(x) | rvalue references / things you're done with | always casts to rvalue |
std::forward<T>(x) | forwarding references | casts to rvalue only if an rvalue was passed |
Type traits (<type_traits>)
Compile-time questions and transformations:
| Trait | Answers / does |
|---|---|
std::is_integral_v<T> | is T an integer type? |
std::is_same_v<T, U> | same type? |
std::is_pointer_v<T>, is_reference_v | pointer/reference? |
std::is_base_of_v<B, D> | inheritance? |
std::is_trivially_copyable_v<T> | safe to memcpy? |
std::remove_reference_t<T> | int& → int |
std::remove_cvref_t<T> | strip const, volatile and references |
std::decay_t<T> | the type as if passed by value |
std::conditional_t<cond, A, B> | pick a type |
Use them with static_assert, if constexpr and concepts.
Example
#include <cstdint>
#include <iostream>
#include <string>
#include <type_traits>
#include <utility>
#include <vector>
void consume(const std::string& s) { std::cout << " got lvalue (copy needed): " << s << '\n'; }
void consume(std::string&& s) { std::cout << " got rvalue (can move): " << s << '\n'; }
template <typename T>
void bad_wrapper(T&& arg) { consume(arg); } // arg has a name -> always an lvalue
template <typename T>
void good_wrapper(T&& arg) { consume(std::forward<T>(arg)); } // preserves the category
template <typename T>
void describe(T&&) {
using Raw = std::remove_cvref_t<T>;
std::cout << " deduced as " << (std::is_lvalue_reference_v<T> ? "lvalue reference" : "non-reference (rvalue)")
<< ", integral? " << std::is_integral_v<Raw> << ", string? " << std::is_same_v<Raw, std::string> << '\n';
}
template <typename T>
using Storage = std::conditional_t<(sizeof(T) <= 8), T, const T&>; // pass small types by value
struct Base {};
struct Derived : Base {};
int main() {
std::string name = "Asha";
std::cout << "bad_wrapper:\n";
bad_wrapper(name);
bad_wrapper(std::string("Ravi")); // the rvalue-ness was lost
std::cout << "good_wrapper:\n";
good_wrapper(name);
good_wrapper(std::string("Ravi"));
std::cout << std::boolalpha << "describe:\n";
int n = 5;
const int cn = 7;
describe(n);
describe(cn);
describe(42);
describe(name);
std::cout << "traits:\n";
std::cout << " is_same<int, int32_t>: " << std::is_same_v<int, std::int32_t> << '\n';
std::cout << " is_base_of<Base, Derived>: " << std::is_base_of_v<Base, Derived> << '\n';
std::cout << " is_trivially_copyable<std::string>: " << std::is_trivially_copyable_v<std::string>
<< ", <double>: " << std::is_trivially_copyable_v<double> << '\n';
std::cout << " Storage<double> is a reference? " << std::is_reference_v<Storage<double>>
<< ", Storage<std::vector<int>>? " << std::is_reference_v<Storage<std::vector<int>>> << '\n';
static_assert(std::is_same_v<std::decay_t<const int&>, int>);
return 0;
}
/* Output:
bad_wrapper:
got lvalue (copy needed): Asha
got lvalue (copy needed): Ravi
good_wrapper:
got lvalue (copy needed): Asha
got rvalue (can move): Ravi
describe:
deduced as lvalue reference, integral? true, string? false
deduced as lvalue reference, integral? true, string? false
deduced as non-reference (rvalue), integral? true, string? false
deduced as lvalue reference, integral? false, string? true
traits:
is_same<int, int32_t>: true
is_base_of<Base, Derived>: true
is_trivially_copyable<std::string>: false, <double>: true
Storage<double> is a reference? false, Storage<std::vector<int>>? true
*/