Step 2 of 6
Perfect forwarding
A factory like std::make_unique<T>(args...) must pass its arguments to T's constructor exactly as received: temporaries as rvalues (so they can be moved), named variables as lvalues (so they're copied, not stolen).
That's perfect forwarding:
template <typename T, typename... Args>
std::unique_ptr<T> make(Args&&... args) {
return std::unique_ptr<T>(new T(std::forward<Args>(args)...));
}
Args&&in a template is a forwarding reference: it binds to anything and remembers whether it was an lvalue or an rvalue.std::forward<Args>(args)...restores that category when passing it on.
Your turn: write template <typename T, typename... Args> T& emplace_into(std::vector<T>& v, Args&&... args) that constructs a T at the end of v from the forwarded arguments (with emplace_back) and returns a reference to it.
Previous: Parameter packs and fold expressions Next: Type traits