A new WG21 paper is available. A copy is linked below, and the paper will also appear in the next normal WG21 mailing. If you are not a committee member, please use the comments section below or the std-proposals forum for public discussion.
Document number: N3588
Date: 2013-03-15
make_unique
by Stephan T. Lavavej
Excerpt:
I. Introduction
This is a proposal to add
make_unique
for symmetry, simplicity, and safety.II. Motivation And Scope
C++11 provided
make_shared
forshared_ptr
, but notmake_unique
forunique_ptr
. This is widely viewed as an oversight. While it isn't absolutely necessary for the Standard to providemake_unique
(because skilled users can implement it with perfect efficiency), it's still important. The implementation ofmake_unique<T>(args...)
, a perfectly forwarding variadic template, is difficult for beginners to understand.make_unique<T[]>
involves even more subtleties. Becausemake_unique
is commonly desired, adding it to the Standard Library will put an end to the proliferation of user implementations.
make_unique
's presence in the Standard Library will have several wonderful consequences. It will be possible to teach users "never saynew/delete /new[]/delete[]
" without disclaimers. Additionally,make_unique
shares two advantages withmake_shared
(excluding the third advantage, increased efficiency). First,unique_ptr<LongTypeName> up(new LongTypeName(args))
must mentionLongTypeName
twice, whileauto up = make_unique<LongTypeName>(args)
mentions it once. Second,make_unique
prevents the unspecified-evaluation-order leak triggered by expressions likefoo(unique_ptr<X>(new X), unique_ptr<Y>(new Y))
. (Following the advice "never saynew
" is simpler than "never saynew
, unless you immediately give it to a namedunique_ptr
".)
Add a Comment
Comments are closed.