1 // Copyright (C) 2016-2018 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 From>
can_assign()23 constexpr bool can_assign()
24 { return std::is_assignable<std::shared_ptr<T>, From>::value; }
25 
26 struct Base { };
27 struct Derived : Base { };
28 
29 // Positive cases:
30 
31 static_assert( can_assign<const void, const std::shared_ptr<void>&>(),
32     "void* convertible to const void*");
33 static_assert( can_assign<const void, std::shared_ptr<void>&&>(),
34     "void* convertible to const void*");
35 static_assert( can_assign<const int, std::shared_ptr<int>>(),
36     "int* convertible to const int*");
37 static_assert( can_assign<Base, std::shared_ptr<Derived>>(),
38     "Derived* convertible to Base*");
39 static_assert( can_assign<const Base, std::shared_ptr<Derived>>(),
40     "Derived* convertible to const Base*");
41 
42 // Negative cases:
43 
44 static_assert( !can_assign<int, const std::shared_ptr<void>&>(),
45     "void* not convertible to int*");
46 static_assert( !can_assign<int, std::shared_ptr<void>&&>(),
47     "void* not convertible to int*");
48 
49 static_assert( !can_assign<int, const std::shared_ptr<const int>&>(),
50     "const int* not convertible to int*");
51 static_assert( !can_assign<int, std::shared_ptr<const int>&&>(),
52     "const int* not convertible to int*");
53 
54 static_assert( !can_assign<int, const std::shared_ptr<long>&>(),
55     "long* not convertible to int*");
56 static_assert( !can_assign<int, std::shared_ptr<long>&&>(),
57     "long* not convertible to int*");
58 
59 static_assert( !can_assign<int, std::unique_ptr<long>&&>(),
60     "unique_ptr<long>::pointer not convertible to int*");
61 
62 static_assert( !can_assign<Derived, const std::shared_ptr<Base>&>(),
63     "Base* not convertible to Derived*");
64 static_assert( !can_assign<int, std::shared_ptr<long>&&>(),
65     "Base* not convertible to Derived*");
66 static_assert( !can_assign<Derived, std::unique_ptr<Base>&&>(),
67     "unique_ptr<Base>::pointer not convertible to Derived*");
68 
69 struct Deleter {
70   using pointer = void*;
operator ()Deleter71   void operator()(pointer) const { }
72 };
73 
74 static_assert( !can_assign<Derived, std::unique_ptr<Derived, Deleter>&&>(),
75     "unique_ptr<Derived, Deleter>::pointer not convertible to Derived*");
76