| 1 | // Copyright (C) 2018-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++20 } } |
| 19 | // { dg-add-options no_pch } |
| 20 | |
| 21 | #include <type_traits> |
| 22 | |
| 23 | #ifndef __cpp_lib_unwrap_ref |
| 24 | # error "Feature-test macro for unwrap_reference missing in <type_traits>" |
| 25 | #elif __cpp_lib_unwrap_ref != 201811L |
| 26 | # error "Feature-test macro for unwrap_reference has wrong value in <type_traits>" |
| 27 | #endif |
| 28 | |
| 29 | template<typename T, typename U> struct expect_same; |
| 30 | template<typename T> struct expect_same<T, T> : std::true_type { }; |
| 31 | |
| 32 | template<typename T, typename U = T> |
| 33 | constexpr bool check() |
| 34 | { |
| 35 | using std::unwrap_reference; |
| 36 | using T2 = typename unwrap_reference<T>::type; |
| 37 | static_assert(expect_same<T2, std::unwrap_reference_t<T2>>::value); |
| 38 | return expect_same<T2, U>::value; |
| 39 | } |
| 40 | |
| 41 | void |
| 42 | test01() |
| 43 | { |
| 44 | static_assert( check<int>() ); |
| 45 | static_assert( check<const int>() ); |
| 46 | static_assert( check<const int&>() ); |
| 47 | static_assert( check<const int*>() ); |
| 48 | static_assert( check<const int*&>() ); |
| 49 | |
| 50 | // reference_wrapper types should get unwrapped: |
| 51 | static_assert( check<std::reference_wrapper<int>, int&>() ); |
| 52 | static_assert( check<std::reference_wrapper<const int>, const int&>() ); |
| 53 | static_assert( check<std::reference_wrapper<long>, long&>() ); |
| 54 | |
| 55 | // But not cv-qualified reference_wrapper types: |
| 56 | static_assert( check<const std::reference_wrapper<int>>() ); |
| 57 | static_assert( check<volatile std::reference_wrapper<int>>() ); |
| 58 | static_assert( check<const volatile std::reference_wrapper<int>>() ); |
| 59 | |
| 60 | // Or references to reference_wrapper types: |
| 61 | static_assert( check<std::reference_wrapper<int>&>() ); |
| 62 | static_assert( check<std::reference_wrapper<int>&&>() ); |
| 63 | static_assert( check<const std::reference_wrapper<int>&>() ); |
| 64 | } |