1 // Copyright (C) 2016-2019 Free Software Foundation, Inc.
2 //
3 // This file is part of the GNU ISO C++ Library.  This library is free
4 // software; you can redistribute it and/or modify it under the
5 // terms of the GNU General Public License as published by the
6 // Free Software Foundation; either version 3, or (at your option)
7 // any later version.
8 
9 // This library is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 // GNU General Public License for more details.
13 
14 // You should have received a copy of the GNU General Public License along
15 // with this library; see the file COPYING3.  If not see
16 // <http://www.gnu.org/licenses/>.
17 
18 // { dg-do compile { target c++11 } }
19 
20 #include <memory>
21 
22 template<typename T, typename Args, typename = void>
23   struct resettable
24   : std::false_type
25   { };
26 
27 template<typename... T> struct type_list { };
28 
29 template<typename T, typename... Args>
30   using reset_result
31     = decltype(std::shared_ptr<T>{}.reset(std::declval<Args>()...));
32 
33 template<typename T, typename... Args>
34   struct resettable<T, type_list<Args...>, reset_result<T, Args...>>
35   : std::true_type
36   { };
37 
38 template<typename T, typename... Args>
can_reset()39 constexpr bool can_reset()
40 { return resettable<T, type_list<Args...>>::value; }
41 
42 template<typename T>
43 struct Deleter {
44   void operator()(T*) const;
45 };
46 
47 template<typename T>
48 using Alloc = std::allocator<T>;
49 
50 struct Base { };
51 struct Derived : Base { };
52 
53 // Positive cases:
54 
55 static_assert( can_reset<const void, void*>(),
56     "void* convertible to const void*");
57 static_assert( can_reset<const int, int*>(),
58     "int* convertible to const int*");
59 static_assert( can_reset<Base, Derived*>(),
60     "Derived* convertible to Base*");
61 static_assert( can_reset<const Base, Derived*>(),
62     "Derived* convertible to const Base*");
63 
64 // Negative cases:
65 
66 static_assert( !can_reset<int, void*>(),
67     "void* not convertible to int*");
68 static_assert( !can_reset<int, void*, Deleter<int>>(),
69     "void* not convertible to int*");
70 static_assert( !can_reset<int, void*, Deleter<int>, Alloc<int>>(),
71     "void* not convertible to int*");
72 
73 static_assert( !can_reset<int, const int*>(),
74     "const int* not convertible to int*");
75 static_assert( !can_reset<int, const int*, Deleter<int>>(),
76     "const int* not convertible to int*");
77 static_assert( !can_reset<int, const int*, Deleter<int>, Alloc<int>>(),
78     "const int* not convertible to int*");
79 
80 static_assert( !can_reset<int, long*>(),
81     "long* not convertible to int*");
82 static_assert( !can_reset<int, long*, Deleter<int>>(),
83     "long* not convertible to int*");
84 static_assert( !can_reset<int, long*, Deleter<int>, Alloc<int>>(),
85     "long* not convertible to int*");
86 
87 static_assert( !can_reset<Derived, Base*>(),
88     "Base* not convertible to Derived*");
89 static_assert( !can_reset<Derived, Base*, Deleter<int>>(),
90     "Base* not convertible to Derived*");
91 static_assert( !can_reset<Derived, Base*, Deleter<int>, Alloc<int>>(),
92     "Base* not convertible to Derived*");
93