Recreating make_unique Function in C 11
The C 11 standard introduces a powerful function, make_unique, for creating unique pointers. However, some may encounter the issue of their compiler not supporting this function. This article provides a workaround to implement make_unique in C 11.
As per the question, the make_unique function prototype is:
template unique_ptr make_unique( Args&&&... args );
The following code provides an implementation of make_unique:
template
std::unique_ptr make_unique(Args&&... args)
{
return std::unique_ptr(new T(std::forward(args)...));
}
This implementation uses the std::forward function to ensure that argument forwarding occurs correctly for all types of arguments, including references and rvalue references.
Note that if your compiler supports C 11 but not the make_unique function, you can still use this implementation as a workaround. Alternatively, if you have access to a compiler that supports C 14 or later, you can simply leverage the standard std::make_unique function for this purpose.
Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.
Copyright© 2022 湘ICP备2022001581号-3