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_uniquefor symmetry, simplicity, and safety.II. Motivation And Scope
C++11 provided
make_sharedforshared_ptr, but notmake_uniqueforunique_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_uniqueis 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_uniqueshares two advantages withmake_shared(excluding the third advantage, increased efficiency). First,unique_ptr<LongTypeName> up(new LongTypeName(args))must mentionLongTypeNametwice, whileauto up = make_unique<LongTypeName>(args)mentions it once. Second,make_uniqueprevents 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.