Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does C++11 have `make_shared` but not `make_unique` [duplicate]

Tags:

c++

c++11

Possible Duplicate:
make_unique and perfect forwarding

Why does C++11 have a make_shared template, but not a make_unique template?

This makes code very inconsistent.

auto x = make_shared<string>("abc"); auto y = unique_ptr<string>(new string("abc")); 
like image 239
Šimon Tóth Avatar asked Sep 25 '12 09:09

Šimon Tóth


1 Answers

According to Herb Sutter in this article it was "partly an oversight". The article contains a nice implementation, and makes a strong case for using it:

template<typename T, typename ...Args> std::unique_ptr<T> make_unique( Args&& ...args ) {     return std::unique_ptr<T>( new T( std::forward<Args>(args)... ) ); } 

Update: The original update has been updated and the emphasis has changed.

like image 88
juanchopanza Avatar answered Oct 21 '22 14:10

juanchopanza