std::make_unique in C++ 14 Last Updated : 22 Sep, 2023 Comments Improve Suggest changes 1 Likes Like Report std::make_unique is a utility function in C++ that was introduced in C++14. It is used to create a unique_ptr object, which is a smart pointer that manages the lifetime of dynamically allocated objects. It is defined inside <memory> header file. Syntaxstd::make_unique <object_type> (arguments);Parametersobject_type: It is the type of object you want to create.arguments: It is the argument list for the constructor of object_type.Return TypeThis function returns a unique_ptr of type object_type.It is the preferred way to create a std::unique_ptr, as it is safer than using the new operator directly because the object is automatically destroyed when it goes out of scope. Examples of std::make_uniqueThe following programs demonstrate how to implement std::make_unique() in our programs. Example 1 C++ // C++ code to implement std::make_unique() #include <iostream> #include <memory> using namespace std; class Geeks { public: Geeks() { cout << "Object Created\n"; } // constructor ~Geeks() { cout << "Object Destroyed"; } // destructor }; void f() { // creating unique ptr object unique_ptr<Geeks> o = make_unique<Geeks>(); } int main() { f(); return 0; } OutputObject Created Object Destroyed Example 2 C++14 // C++ code to implement std::make_unique() #include <iostream> #include <memory> using namespace std; class Geeks { public: int d; // construtor Geeks(int x) { this->d = x; cout << "Object Created\n"; } // destructor ~Geeks() { cout << "Object Destroyed"; } }; void f() { // creating unique ptr object unique_ptr<Geeks> o = make_unique<Geeks>(10); cout << o->d << endl; } int main() { f(); return 0; } OutputObject Created 10 Object Destroyed Advantages of std::make_uniqueUnlike new, make_unique is exception safe.No cleanup is necessary if make_unique is not evaluated. Create Quiz Comment A as975862 Follow 1 Improve A as975862 Follow 1 Improve Article Tags : C++ Geeks Premier League C++ 14 Geeks Premier League 2023 Explore C++ BasicsIntroduction to C++3 min readData Types in C++6 min readVariables in C++4 min readOperators in C++9 min readBasic Input / Output in C++3 min readControl flow statements in Programming15+ min readLoops in C++7 min readFunctions in C++8 min readArrays in C++8 min readCore ConceptsPointers and References in C++5 min readnew and delete Operators in C++ For Dynamic Memory5 min readTemplates in C++8 min readStructures, Unions and Enumerations in C++3 min readException Handling in C++12 min readFile Handling in C++8 min readMultithreading in C++8 min readNamespace in C++5 min readOOP in C++Object Oriented Programming in C++8 min readInheritance in C++6 min readPolymorphism in C++5 min readEncapsulation in C++3 min readAbstraction in C++4 min readStandard Template Library(STL)Standard Template Library (STL) in C++3 min readContainers in C++ STL2 min readIterators in C++ STL10 min readC++ STL Algorithm Library3 min readPractice & ProblemsC++ Interview Questions and Answers1 min readC++ Programming Examples4 min read Like