Title
shared_ptr's constructor from unique_ptr should be constrained
Status
c++17
Section
[util.smartptr.shared.const]
Submitter
Stephan T. Lavavej

Created on 2014-06-12.00:00:00 last changed 90 months ago

Messages

Date: 2014-06-16.23:41:12

Proposed resolution:

This wording is relative to N3936.

  1. Change [util.smartptr.shared.const] around p33 as indicated:

    template <class Y, class D> shared_ptr(unique_ptr<Y, D>&& r);
    

    -?- Remark: This constructor shall not participate in overload resolution unless unique_ptr<Y, D>::pointer is convertible to T*.

    -33- Effects: Equivalent to shared_ptr(r.release(), r.get_deleter()) when D is not a reference type, otherwise shared_ptr(r.release(), ref(r.get_deleter())).

Date: 2014-06-16.00:00:00

[ 2014-06-16 Rapperswil ]

Move to Ready

Date: 2014-06-16.23:41:12

Consider the following code:

#include <iostream>
#include <memory>
#include <string>

using namespace std;

void meow(const shared_ptr<int>& sp) {
  cout << "int: " << *sp << endl;
}

void meow(const shared_ptr<string>& sp) {
  cout << "string: " << *sp << endl;
}

int main() {
  meow(make_unique<int>(1729));
  meow(make_unique<string>("kitty"));
}

This fails to compile due to ambiguous overload resolution, but we can easily make this work. (Note: shared_ptr's constructor from auto_ptr is also affected, but I believe that it's time to remove auto_ptr completely.)

History
Date User Action Args
2017-07-30 20:15:43adminsetstatus: wp -> c++17
2014-11-08 19:44:42adminsetstatus: voting -> wp
2014-11-04 10:26:50adminsetstatus: ready -> voting
2014-06-16 23:41:12adminsetmessages: + msg7052
2014-06-16 23:41:12adminsetstatus: new -> ready
2014-06-14 17:11:17adminsetmessages: + msg7029
2014-06-12 00:00:00admincreate