Title
Default-constructibility of unique_ptr
Status
c++17
Section
[unique.ptr.single.ctor]
Submitter
United States

Created on 2016-11-09.00:00:00 last changed 81 months ago

Messages

Date: 2017-03-03.22:19:58

Proposed resolution:

  1. Modify the synopsis in [unique.ptr.single] as follows:

    […]
    constexpr unique_ptr(nullptr_t) noexcept;
        : unique_ptr() { }
    […]
    
  2. Modify [unique.ptr.single.ctor] as follows:

    constexpr unique_ptr() noexcept;
    constexpr unique_ptr(nullptr_t) noexcept;
    

    -1- Requires: D shall satisfy the requirements of DefaultConstructible (Table 22), and that construction shall not throw an exception.

    -2- Effects: Constructs a unique_ptr object that owns nothing, value-initializing the stored pointer and the stored deleter.

    -3- Postconditions: get() == nullptr. get_deleter() returns a reference to the stored deleter.

    -4- Remarks: If this constructor is instantiated with a pointer type or reference type for the template argument D, the program is ill-formed. If is_pointer_v<deleter_type> is true or is_default_constructible_v<deleter_type> is false, this constructor shall not participate in overload resolution.

    explicit unique_ptr(pointer p) noexcept;
    

    […]

    -8- Remarks: If this constructor is instantiated with a pointer type or reference type for the template argument D, the program is ill-formed. If is_pointer_v<deleter_type> is true or is_default_constructible_v<deleter_type> is false, this constructor shall not participate in overload resolution.

  3. Modify the synopsis in [unique.ptr.runtime] as follows:

    […]
    constexpr unique_ptr(nullptr_t) noexcept; : unique_ptr() { }
    […]
    
  4. Modify [unique.ptr.runtime.ctor] as follows:

    template <class U> explicit unique_ptr(U p) noexcept;
    

    This constructor behaves the same as the constructor that takes a pointer parameter in the primary template except that the following constraints are added for it to participate in overload resolution

    • U is the same type as pointer, or

    • pointer is the same type as element_type*, U is a pointer type V*, and V(*)[] is convertible to element_type(*)[].

    template <class U> unique_ptr(U p, see below d) noexcept;
    template <class U> unique_ptr(U p, see below d) noexcept;
    

    1 These constructors behave the same as the constructors that take a pointer parameter in the primary template except that they shall not participate in overload resolution unless either

Date: 2017-03-03.22:19:58

[ Kona 2017-03-02 ]

Accepted as Immediate to resolve NB comment.

Date: 2017-03-15.00:00:00

[ 2017-03-02, Kona, STL comments and tweaks the wording ]

LWG discussed this issue, and we liked it, but we wanted to tweak the PR. I ran this past Ville (who drafted the PR with Howard), and he was in favor of tweaking this.

Date: 2016-12-24.00:00:00

[ 2016-12-24: Howard and Ville provided wording. ]

Date: 2017-02-02.00:41:18

[ Issues Telecon 16-Dec-2016 ]

Priority 2; Howard and Ville to provide wording.

Date: 2016-11-09.00:00:00

Addresses US 122

unique_ptr should not satisfy is_constructible_v<unique_ptr<T, D>> unless D is DefaultConstructible and not a pointer type. This is important for interactions with pair, tuple, and variant constructors that rely on the is_default_constructible trait.

Suggested resolution:

Add a Remarks: clause to constrain the default constructor to not exist unless the Requires clause is satisfied.

History
Date User Action Args
2017-07-30 20:15:43adminsetstatus: wp -> c++17
2017-03-05 23:46:08adminsetstatus: immediate -> wp
2017-03-03 22:19:58adminsetmessages: + msg9071
2017-03-03 22:19:58adminsetstatus: new -> immediate
2017-03-02 17:33:29adminsetmessages: + msg9037
2016-12-25 22:40:06adminsetmessages: + msg8771
2016-12-25 22:40:06adminsetmessages: + msg8770
2016-12-16 20:56:38adminsetmessages: + msg8731
2016-11-09 00:00:00admincreate