Q: Why is the centralized make_unique function missing in the C 11 standard library?
Many find the construction boilerplate for unique pointers inconvenient. For example:
std::unique_ptr<SomeUserDefinedType> p(new SomeUserDefinedType(1, 2, 3));
A sleek function like this would be preferred:
auto p = std::make_unique<SomeUserDefinedType>(1, 2, 3);
Q: Here's a make_unique implementation attempt, but something about the std::forward appears broken. What is it doing and how is it supposed to be used?
template<typename T, typename... Args> std::unique_ptr<T> make_unique(Args&&... args) { return std::unique_ptr<T>(new T(std::forward<Args>(args)...)); }
A: The omission of std::make_unique in C 11 was acknowledged as an oversight: it was added in C 14.
The std::forward
The above is the detailed content of Why is `std::make_unique` Missing in C 11, and How Does Perfect Forwarding Work in its Implementation?. For more information, please follow other related articles on the PHP Chinese website!