The C 11 standard library lacks a std::make_unique function template, leaving developers to write verbose code to create unique pointers. Consider the following example:
std::unique_ptr<SomeUserDefinedType> p(new SomeUserDefinedType(1, 2, 3));
To alleviate this verbosity, some developers have proposed a make_unique function:
auto p = std::make_unique<SomeUserDefinedType>(1, 2, 3);
This syntax hides the new operator and mentions the type only once.
However, implementing such a function requires understanding the intricacies of perfect forwarding. Specifically, the std::forward expression:
std::forward<Args>(args)...
involves the following operations:
In summary, std::forward<Args>(args)... allows perfect forwarding of arguments to constructors.
The above is the detailed content of Why is `std::make_unique` a valuable addition to C despite its complexity?. For more information, please follow other related articles on the PHP Chinese website!