2399. shared_ptr's constructor from unique_ptr should be constrained

Section: 23.11.3.1 [util.smartptr.shared.const] Status: C++17 Submitter: Stephan T. Lavavej Opened: 2014-06-14 Last modified: 2017-07-30

Priority: 0

View all other issues in [util.smartptr.shared.const].

View all issues with C++17 status.

Discussion:

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.)

[2014-06-16 Rapperswil]

Move to Ready

Proposed resolution:

This wording is relative to N3936.

  1. Change 23.11.3.1 [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())).