Skip to content

Commit d868f09

Browse files
authored
[libc++] LWG3223 Broken requirements for shared_ptr converting constructors (#93071)
1 parent 44d4b3b commit d868f09

File tree

7 files changed

+113
-77
lines changed

7 files changed

+113
-77
lines changed

libcxx/docs/Status/Cxx20Issues.csv

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@
200200
"`3200 <https://wg21.link/LWG3200>`__","``midpoint``\ should not constrain ``T``\ is complete","Prague","|Nothing To Do|",""
201201
"`3201 <https://wg21.link/LWG3201>`__","``lerp``\ should be marked as ``noexcept``\ ","Prague","|Complete|",""
202202
"`3226 <https://wg21.link/LWG3226>`__","``zoned_time``\ constructor from ``string_view``\ should accept ``zoned_time<Duration2, TimeZonePtr2>``\ ","Prague","","","|chrono|"
203-
"`3233 <https://wg21.link/LWG3233>`__","Broken requirements for ``shared_ptr``\ converting constructors","Prague","",""
203+
"`3233 <https://wg21.link/LWG3233>`__","Broken requirements for ``shared_ptr``\ converting constructors","Prague","|Complete|","19.0"
204204
"`3237 <https://wg21.link/LWG3237>`__","LWG 3038 and 3190 have inconsistent PRs","Prague","|Complete|","16.0"
205205
"`3238 <https://wg21.link/LWG3238>`__","Insufficiently-defined behavior of ``std::function``\ deduction guides","Prague","",""
206206
"`3242 <https://wg21.link/LWG3242>`__","``std::format``\ : missing rules for ``arg-id``\ in ``width``\ and ``precision``\ ","Prague","|Complete|","14.0","|format|"

libcxx/include/__memory/shared_ptr.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -403,6 +403,9 @@ struct __shared_ptr_deleter_ctor_reqs {
403403
__well_formed_deleter<_Dp, _Yp*>::value;
404404
};
405405

406+
template <class _Dp, class _Tp>
407+
using __shared_ptr_nullptr_deleter_ctor_reqs = _And<is_move_constructible<_Dp>, __well_formed_deleter<_Dp, nullptr_t> >;
408+
406409
#if defined(_LIBCPP_ABI_ENABLE_SHARED_PTR_TRIVIAL_ABI)
407410
# define _LIBCPP_SHARED_PTR_TRIVIAL_ABI __attribute__((__trivial_abi__))
408411
#else
@@ -498,7 +501,7 @@ class _LIBCPP_SHARED_PTR_TRIVIAL_ABI _LIBCPP_TEMPLATE_VIS shared_ptr {
498501
#endif // _LIBCPP_HAS_NO_EXCEPTIONS
499502
}
500503

501-
template <class _Dp>
504+
template <class _Dp, __enable_if_t<__shared_ptr_nullptr_deleter_ctor_reqs<_Dp, _Tp>::value, int> = 0 >
502505
_LIBCPP_HIDE_FROM_ABI shared_ptr(nullptr_t __p, _Dp __d) : __ptr_(nullptr) {
503506
#ifndef _LIBCPP_HAS_NO_EXCEPTIONS
504507
try {
@@ -518,7 +521,7 @@ class _LIBCPP_SHARED_PTR_TRIVIAL_ABI _LIBCPP_TEMPLATE_VIS shared_ptr {
518521
#endif // _LIBCPP_HAS_NO_EXCEPTIONS
519522
}
520523

521-
template <class _Dp, class _Alloc>
524+
template <class _Dp, class _Alloc, __enable_if_t<__shared_ptr_nullptr_deleter_ctor_reqs<_Dp, _Tp>::value, int> = 0 >
522525
_LIBCPP_HIDE_FROM_ABI shared_ptr(nullptr_t __p, _Dp __d, _Alloc __a) : __ptr_(nullptr) {
523526
#ifndef _LIBCPP_HAS_NO_EXCEPTIONS
524527
try {

libcxx/test/std/utilities/memory/util.smartptr/util.smartptr.shared/util.smartptr.shared.const/nullptr_t_deleter.pass.cpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include "test_macros.h"
1818
#include "deleter_types.h"
1919

20+
#include "types.h"
2021
struct A
2122
{
2223
static int count;
@@ -28,6 +29,25 @@ struct A
2829

2930
int A::count = 0;
3031

32+
// LWG 3233. Broken requirements for shared_ptr converting constructors
33+
// https://cplusplus.github.io/LWG/issue3233
34+
static_assert( std::is_constructible<std::shared_ptr<int>, std::nullptr_t, test_deleter<int> >::value, "");
35+
static_assert(!std::is_constructible<std::shared_ptr<int>, std::nullptr_t, bad_deleter>::value, "");
36+
static_assert(!std::is_constructible<std::shared_ptr<int>, std::nullptr_t, no_nullptr_deleter>::value, "");
37+
static_assert(!std::is_constructible<std::shared_ptr<int>, std::nullptr_t, no_move_deleter>::value, "");
38+
39+
#if TEST_STD_VER >= 17
40+
static_assert( std::is_constructible<std::shared_ptr<int[]>, std::nullptr_t, test_deleter<int> >::value, "");
41+
static_assert(!std::is_constructible<std::shared_ptr<int[]>, std::nullptr_t, bad_deleter>::value, "");
42+
static_assert(!std::is_constructible<std::shared_ptr<int[]>, std::nullptr_t, no_nullptr_deleter>::value, "");
43+
static_assert(!std::is_constructible<std::shared_ptr<int[]>, std::nullptr_t, no_move_deleter>::value, "");
44+
45+
static_assert( std::is_constructible<std::shared_ptr<int[5]>, std::nullptr_t, test_deleter<int> >::value, "");
46+
static_assert(!std::is_constructible<std::shared_ptr<int[5]>, std::nullptr_t, bad_deleter>::value, "");
47+
static_assert(!std::is_constructible<std::shared_ptr<int[5]>, std::nullptr_t, no_nullptr_deleter>::value, "");
48+
static_assert(!std::is_constructible<std::shared_ptr<int[5]>, std::nullptr_t, no_move_deleter>::value, "");
49+
#endif
50+
3151
int main(int, char**)
3252
{
3353
{

libcxx/test/std/utilities/memory/util.smartptr/util.smartptr.shared/util.smartptr.shared.const/nullptr_t_deleter_allocator.pass.cpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
#include "test_allocator.h"
1818
#include "min_allocator.h"
1919

20+
#include "types.h"
21+
2022
struct A
2123
{
2224
static int count;
@@ -28,6 +30,25 @@ struct A
2830

2931
int A::count = 0;
3032

33+
// LWG 3233. Broken requirements for shared_ptr converting constructors
34+
// https://cplusplus.github.io/LWG/issue3233
35+
static_assert( std::is_constructible<std::shared_ptr<int>, std::nullptr_t, test_deleter<int>, test_allocator<int> >::value, "");
36+
static_assert(!std::is_constructible<std::shared_ptr<int>, std::nullptr_t, bad_deleter, test_allocator<int> >::value, "");
37+
static_assert(!std::is_constructible<std::shared_ptr<int>, std::nullptr_t, no_nullptr_deleter, test_allocator<int> >::value, "");
38+
static_assert(!std::is_constructible<std::shared_ptr<int>, std::nullptr_t, no_move_deleter, test_allocator<int> >::value, "");
39+
40+
#if TEST_STD_VER >= 17
41+
static_assert( std::is_constructible<std::shared_ptr<int[]>, std::nullptr_t, test_deleter<int>, test_allocator<int> >::value, "");
42+
static_assert(!std::is_constructible<std::shared_ptr<int[]>, std::nullptr_t, bad_deleter, test_allocator<int> >::value, "");
43+
static_assert(!std::is_constructible<std::shared_ptr<int[]>, std::nullptr_t, no_nullptr_deleter, test_allocator<int> >::value, "");
44+
static_assert(!std::is_constructible<std::shared_ptr<int[]>, std::nullptr_t, no_move_deleter, test_allocator<int> >::value, "");
45+
46+
static_assert( std::is_constructible<std::shared_ptr<int[5]>, std::nullptr_t, test_deleter<int>, test_allocator<int> >::value, "");
47+
static_assert(!std::is_constructible<std::shared_ptr<int[5]>, std::nullptr_t, bad_deleter, test_allocator<int> >::value, "");
48+
static_assert(!std::is_constructible<std::shared_ptr<int[5]>, std::nullptr_t, no_nullptr_deleter, test_allocator<int> >::value, "");
49+
static_assert(!std::is_constructible<std::shared_ptr<int[5]>, std::nullptr_t, no_move_deleter, test_allocator<int> >::value, "");
50+
#endif
51+
3152
int main(int, char**)
3253
{
3354
test_allocator_statistics alloc_stats;

libcxx/test/std/utilities/memory/util.smartptr/util.smartptr.shared/util.smartptr.shared.const/pointer_deleter.pass.cpp

Lines changed: 8 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
#include "test_macros.h"
1818
#include "deleter_types.h"
1919

20+
#include "types.h"
21+
2022
struct A
2123
{
2224
static int count;
@@ -28,38 +30,8 @@ struct A
2830

2931
int A::count = 0;
3032

31-
struct bad_ty { };
32-
33-
struct bad_deleter
34-
{
35-
void operator()(bad_ty) { }
36-
};
37-
38-
struct no_move_deleter
39-
{
40-
no_move_deleter(no_move_deleter const&) = delete;
41-
no_move_deleter(no_move_deleter &&) = delete;
42-
void operator()(int*) { }
43-
};
44-
45-
static_assert(!std::is_move_constructible<no_move_deleter>::value, "");
46-
47-
struct Base { };
48-
struct Derived : Base { };
49-
50-
template<class T>
51-
class MoveDeleter
52-
{
53-
MoveDeleter();
54-
MoveDeleter(MoveDeleter const&);
55-
public:
56-
MoveDeleter(MoveDeleter&&) {}
57-
58-
explicit MoveDeleter(int) {}
59-
60-
void operator()(T* ptr) { delete ptr; }
61-
};
62-
33+
// LWG 3233. Broken requirements for shared_ptr converting constructors
34+
// https://cplusplus.github.io/LWG/issue3233
6335
// https://llvm.org/PR60258
6436
// Invalid constructor SFINAE for std::shared_ptr's array ctors
6537
static_assert( std::is_constructible<std::shared_ptr<int>, int*, test_deleter<int> >::value, "");
@@ -68,12 +40,12 @@ static_assert( std::is_constructible<std::shared_ptr<Base>, Derived*, test_dele
6840
static_assert(!std::is_constructible<std::shared_ptr<A>, int*, test_deleter<A> >::value, "");
6941

7042
#if TEST_STD_VER >= 17
71-
static_assert( std::is_constructible<std::shared_ptr<int[]>, int*, test_deleter<int>>::value, "");
43+
static_assert( std::is_constructible<std::shared_ptr<int[]>, int*, test_deleter<int> >::value, "");
7244
static_assert(!std::is_constructible<std::shared_ptr<int[]>, int*, bad_deleter>::value, "");
73-
static_assert(!std::is_constructible<std::shared_ptr<int[]>, int(*)[], test_deleter<int>>::value, "");
74-
static_assert( std::is_constructible<std::shared_ptr<int[5]>, int*, test_deleter<int>>::value, "");
45+
static_assert(!std::is_constructible<std::shared_ptr<int[]>, int(*)[], test_deleter<int> >::value, "");
46+
static_assert( std::is_constructible<std::shared_ptr<int[5]>, int*, test_deleter<int> >::value, "");
7547
static_assert(!std::is_constructible<std::shared_ptr<int[5]>, int*, bad_deleter>::value, "");
76-
static_assert(!std::is_constructible<std::shared_ptr<int[5]>, int(*)[5], test_deleter<int>>::value, "");
48+
static_assert(!std::is_constructible<std::shared_ptr<int[5]>, int(*)[5], test_deleter<int> >::value, "");
7749
#endif
7850

7951
int main(int, char**)

libcxx/test/std/utilities/memory/util.smartptr/util.smartptr.shared/util.smartptr.shared.const/pointer_deleter_allocator.pass.cpp

Lines changed: 9 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include "test_allocator.h"
1818
#include "min_allocator.h"
1919

20+
#include "types.h"
2021
struct A
2122
{
2223
static int count;
@@ -28,38 +29,8 @@ struct A
2829

2930
int A::count = 0;
3031

31-
struct bad_ty { };
32-
33-
struct bad_deleter
34-
{
35-
void operator()(bad_ty) { }
36-
};
37-
38-
struct no_move_deleter
39-
{
40-
no_move_deleter(no_move_deleter const&) = delete;
41-
no_move_deleter(no_move_deleter &&) = delete;
42-
void operator()(int*) { }
43-
};
44-
45-
static_assert(!std::is_move_constructible<no_move_deleter>::value, "");
46-
47-
struct Base { };
48-
struct Derived : Base { };
49-
50-
template<class T>
51-
class MoveDeleter
52-
{
53-
MoveDeleter();
54-
MoveDeleter(MoveDeleter const&);
55-
public:
56-
MoveDeleter(MoveDeleter&&) {}
57-
58-
explicit MoveDeleter(int) {}
59-
60-
void operator()(T* ptr) { delete ptr; }
61-
};
62-
32+
// LWG 3233. Broken requirements for shared_ptr converting constructors
33+
// https://cplusplus.github.io/LWG/issue3233
6334
// https://llvm.org/PR60258
6435
// Invalid constructor SFINAE for std::shared_ptr's array ctors
6536
static_assert( std::is_constructible<std::shared_ptr<int>, int*, test_deleter<int>, test_allocator<int> >::value, "");
@@ -68,12 +39,12 @@ static_assert( std::is_constructible<std::shared_ptr<Base>, Derived*, test_dele
6839
static_assert(!std::is_constructible<std::shared_ptr<A>, int*, test_deleter<A>, test_allocator<A> >::value, "");
6940

7041
#if TEST_STD_VER >= 17
71-
static_assert( std::is_constructible<std::shared_ptr<int[]>, int*, test_deleter<int>, test_allocator<int>>::value, "");
72-
static_assert(!std::is_constructible<std::shared_ptr<int[]>, int*, bad_deleter, test_allocator<int>>::value, "");
73-
static_assert(!std::is_constructible<std::shared_ptr<int[]>, int(*)[], test_deleter<int>, test_allocator<int>>::value, "");
74-
static_assert( std::is_constructible<std::shared_ptr<int[5]>, int*, test_deleter<int>, test_allocator<int>>::value, "");
75-
static_assert(!std::is_constructible<std::shared_ptr<int[5]>, int*, bad_deleter, test_allocator<int>>::value, "");
76-
static_assert(!std::is_constructible<std::shared_ptr<int[5]>, int(*)[5], test_deleter<int>, test_allocator<int>>::value, "");
42+
static_assert( std::is_constructible<std::shared_ptr<int[]>, int*, test_deleter<int>, test_allocator<int> >::value, "");
43+
static_assert(!std::is_constructible<std::shared_ptr<int[]>, int*, bad_deleter, test_allocator<int> >::value, "");
44+
static_assert(!std::is_constructible<std::shared_ptr<int[]>, int(*)[], test_deleter<int>, test_allocator<int> >::value, "");
45+
static_assert( std::is_constructible<std::shared_ptr<int[5]>, int*, test_deleter<int>, test_allocator<int> >::value, "");
46+
static_assert(!std::is_constructible<std::shared_ptr<int[5]>, int*, bad_deleter, test_allocator<int> >::value, "");
47+
static_assert(!std::is_constructible<std::shared_ptr<int[5]>, int(*)[5], test_deleter<int>, test_allocator<int> >::value, "");
7748
#endif
7849

7950

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#ifndef TEST_STD_UTILITIES_MEMORY_UTIL_SMARTPTR_SHARED_CONST_TYPES_H
10+
#define TEST_STD_UTILITIES_MEMORY_UTIL_SMARTPTR_SHARED_CONST_TYPES_H
11+
12+
#include <type_traits>
13+
14+
struct bad_ty {};
15+
16+
struct bad_deleter {
17+
void operator()(bad_ty) {}
18+
};
19+
20+
struct no_move_deleter {
21+
no_move_deleter(no_move_deleter const&) = delete;
22+
no_move_deleter(no_move_deleter&&) = delete;
23+
void operator()(int*) {}
24+
};
25+
26+
static_assert(!std::is_move_constructible<no_move_deleter>::value, "");
27+
28+
struct no_nullptr_deleter {
29+
void operator()(int*) const {}
30+
void operator()(std::nullptr_t) const = delete;
31+
};
32+
33+
struct Base {};
34+
struct Derived : Base {};
35+
36+
template <class T>
37+
class MoveDeleter {
38+
MoveDeleter();
39+
MoveDeleter(MoveDeleter const&);
40+
41+
public:
42+
MoveDeleter(MoveDeleter&&) {}
43+
44+
explicit MoveDeleter(int) {}
45+
46+
void operator()(T* ptr) { delete ptr; }
47+
};
48+
49+
#endif // TEST_STD_UTILITIES_MEMORY_UTIL_SMARTPTR_SHARED_CONST_TYPES_H

0 commit comments

Comments
 (0)