https://gcc.gnu.org/bugzilla/show_bug.cgi?id=114354

            Bug ID: 114354
           Summary: std::shared_ptr constructor constraints are checked
                    too late
           Product: gcc
           Version: 14.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: libstdc++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: redi at gcc dot gnu.org
            Blocks: 88322
  Target Milestone: ---

The standard says:

template<class Y> explicit shared_ptr(Y* p);

Constraints: When T is an array type, the expression delete[] p is well-formed
and either T is U[N] and Y(*)[N] is convertible to T*, or T is U[] and Y(*)[]
is convertible to T*.
When T is not an array type, the expression delete p is well-formed and Y* is
convertible to T*.

We do not constrain that constructor with checks for the delete expressions
being well-formed, so this gives the wrong answer:

#include <memory>

struct A {
private:
  ~A();
};

static_assert( ! std::is_constructible_v<std::shared_ptr<A>, A*> );


Similarly for the constructors taking a deleter:


template<class Y, class D> shared_ptr(Y* p, D d);
template<class Y, class D, class A> shared_ptr(Y* p, D d, A a);
template<class D> shared_ptr(nullptr_t p, D d);
template<class D, class A> shared_ptr(nullptr_t p, D d, A a);

Constraints: is_move_constructible_v<D> is true, and d(p) is a well-formed
expression. [...]

We check that with a static assert in the constructor body, which is too late,
so this fails:

struct D { };
static_assert( ! std::is_constructible_v<std::shared_ptr<A>, A*, D> );


Prior to C++20 the requirements were just preconditions, so not required to be
SFINAE-able. So we must fix this for C++20, and could do so for C++11 as QoI.


Referenced Bugs:

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=88322
[Bug 88322] Implement C++20 library features.

Reply via email to