C/C++ Arena

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)...));
}

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