Despite the advantages of reducing verbosity and aiding in type safety, std::make_unique was not included in the C 11 standard. Herb Sutter, chair of the C standardization committee, attributes this omission to an oversight.
In the user-defined make_unique implementation, std::forward
When std::forward is applied to an lvalue reference, it converts it to an rvalue reference, allowing the constructor to bind to an rvalue directly. Conversely, if std::forward is applied to an rvalue reference, it does nothing.
The make_unique implementation creates a new instance of T by allocating memory with new T and then invoking its constructor with the forwarded arguments. The use of perfect forwarding prevents unnecessary copies or unnecessary temporary objects.
Although not part of C 11, std::make_unique has become a widely adopted convention for concisely creating unique pointers. It enhances code readability, simplifies type handling, and ensures efficient resource management. Its anticipated inclusion in C 14 further underscores its importance in modern C programming.
The above is the detailed content of Why Was `std::make_unique` Missing from C 11, and How Does Perfect Forwarding Enhance Its Functionality?. For more information, please follow other related articles on the PHP Chinese website!