| 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 | // REQUIRES: std-at-least-c++23 |
| 10 | |
| 11 | // These compilers don't support std::reference_converts_from_temporary yet. |
| 12 | // UNSUPPORTED: android, apple-clang-15, apple-clang-16, clang-18, clang-19.1 |
| 13 | |
| 14 | // <type_traits> |
| 15 | |
| 16 | // template<class T, class U> struct reference_converts_from_temporary; |
| 17 | |
| 18 | // template<class T, class U> |
| 19 | // constexpr bool reference_converts_from_temporary_v |
| 20 | // = reference_converts_from_temporary<T, U>::value; |
| 21 | |
| 22 | #include <cassert> |
| 23 | #include <type_traits> |
| 24 | |
| 25 | #include "common.h" |
| 26 | #include "test_macros.h" |
| 27 | |
| 28 | template <typename T, typename U, bool Expected> |
| 29 | constexpr void test_reference_converts_from_temporary() { |
| 30 | assert((std::reference_converts_from_temporary<T, U>::value == Expected)); |
| 31 | assert((std::reference_converts_from_temporary_v<T, U> == Expected)); |
| 32 | } |
| 33 | |
| 34 | constexpr bool test() { |
| 35 | test_reference_converts_from_temporary<int&, int&, false>(); |
| 36 | test_reference_converts_from_temporary<int&, int&, false>(); |
| 37 | test_reference_converts_from_temporary<int&, int&&, false>(); |
| 38 | |
| 39 | test_reference_converts_from_temporary<const int&, int&, false>(); |
| 40 | test_reference_converts_from_temporary<const int&, const int&, false>(); |
| 41 | test_reference_converts_from_temporary<const int&, int&&, false>(); |
| 42 | |
| 43 | test_reference_converts_from_temporary<int&, long&, false>(); // doesn't construct |
| 44 | |
| 45 | test_reference_converts_from_temporary<const int&, long&, true>(); |
| 46 | test_reference_converts_from_temporary<const int&, long&&, true>(); |
| 47 | test_reference_converts_from_temporary<int&&, long&, true>(); |
| 48 | |
| 49 | assert((std::is_constructible_v<int&, ConvertsToRef<int, int&>>)); |
| 50 | test_reference_converts_from_temporary<int&, ConvertsToRef<int, int&>, false>(); |
| 51 | |
| 52 | assert((std::is_constructible_v<int&&, ConvertsToRef<int, int&&>>)); |
| 53 | test_reference_converts_from_temporary<int&&, ConvertsToRef<int, int&&>, false>(); |
| 54 | |
| 55 | assert((std::is_constructible_v<const int&, ConvertsToRef<int, const int&>>)); |
| 56 | test_reference_converts_from_temporary<int&&, ConvertsToRef<int, const int&>, false>(); |
| 57 | |
| 58 | assert((std::is_constructible_v<const int&, ConvertsToRef<long, long&>>)); |
| 59 | test_reference_converts_from_temporary<const int&, ConvertsToRef<long, long&>, true>(); |
| 60 | #ifndef TEST_COMPILER_GCC |
| 61 | test_reference_converts_from_temporary<const int&, ConvertsToRefPrivate<long, long&>, false>(); |
| 62 | #endif |
| 63 | |
| 64 | // Test that it doesn't accept non-reference types as input. |
| 65 | test_reference_converts_from_temporary<int, long, false>(); |
| 66 | |
| 67 | test_reference_converts_from_temporary<const int&, long, true>(); |
| 68 | |
| 69 | // Additional checks |
| 70 | test_reference_converts_from_temporary<const Base&, Derived, true>(); |
| 71 | test_reference_converts_from_temporary<int&&, int, true>(); |
| 72 | test_reference_converts_from_temporary<const int&, int, true>(); |
| 73 | test_reference_converts_from_temporary<int&&, int&&, false>(); |
| 74 | test_reference_converts_from_temporary<const int&, int&&, false>(); |
| 75 | test_reference_converts_from_temporary<int&&, long&&, true>(); |
| 76 | test_reference_converts_from_temporary<int&&, long, true>(); |
| 77 | |
| 78 | test_reference_converts_from_temporary<int&, ExplicitConversionRef, false>(); |
| 79 | test_reference_converts_from_temporary<const int&, ExplicitConversionRef, true>(); |
| 80 | test_reference_converts_from_temporary<int&&, ExplicitConversionRvalueRef, true>(); |
| 81 | |
| 82 | return true; |
| 83 | } |
| 84 | |
| 85 | int main(int, char**) { |
| 86 | test(); |
| 87 | static_assert(test()); |
| 88 | |
| 89 | return 0; |
| 90 | } |
| 91 | |