Why C ++ 11 has `make_shared` but not` make_unique`

Possible duplicate:
make_unique and perfect redirection

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

This makes the code very inconsistent.

 auto x = make_shared<string>("abc"); auto y = unique_ptr<string>(new string("abc")); 
+40
c ++ c ++ 11
Sep 25 '12 at 9:50
source share
1 answer

According to Herb Sutter in this article , it was "partially oversight." This article contains a nice implementation and provides a convincing argument in favor of its use:

 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 is updated, and the emphasis has been changed.

+49
Sep 25 '12 at 9:52
source share



All Articles