Implementing make_unique with perfect forwarding in C++
This is a medium-difficulty C++ coding problem that tests your understanding of perfect forwarding and smart-pointer semantics. It's a canonical interview question at quant firms and other performance-sensitive shops where candidates must demonstrate command of modern C++ idioms.
The core challenge is to write a template function that forwards constructor arguments to an object of type T, wrapping the result in a std::unique_ptr, while preserving the value category (lvalue or rvalue) of each argument. This is essential for respecting move semantics and avoiding unnecessary copies. Your solution will be validated against a test object that counts construction, copy, and move events, so correctness of forwarding directly impacts the output.
- Universal references and reference collapsing
- The
std::forward utility and its role in perfect forwarding
- Ownership semantics of
std::unique_ptr
- Template argument deduction with variadic parameter packs