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