|
| 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 | +// UNSUPPORTED: std-at-least-23 |
| 10 | + |
| 11 | +// <type_traits> |
| 12 | + |
| 13 | +// template<class T, class U> struct reference_constructs_from_temporary; |
| 14 | + |
| 15 | +// template<class T, class U> |
| 16 | +// constexpr bool reference_constructs_from_temporary_v |
| 17 | +// = reference_constructs_from_temporary<T, U>::value; |
| 18 | + |
| 19 | +#include <cassert> |
| 20 | +#include <type_traits> |
| 21 | + |
| 22 | +struct NonPOD { |
| 23 | + NonPOD(int); |
| 24 | +}; |
| 25 | +enum Enum { EV }; |
| 26 | +struct POD { |
| 27 | + Enum e; |
| 28 | + int i; |
| 29 | + float f; |
| 30 | + NonPOD* p; |
| 31 | +}; |
| 32 | +// Not PODs |
| 33 | +struct Derives : POD {}; |
| 34 | + |
| 35 | +template <class T, class RefType = T&> |
| 36 | +struct ConvertsToRef { |
| 37 | + operator RefType() const { return static_cast<RefType>(obj); } |
| 38 | + mutable T obj = 42; |
| 39 | +}; |
| 40 | +template <class T, class RefType = T&> |
| 41 | +class ConvertsToRefPrivate { |
| 42 | + operator RefType() const { return static_cast<RefType>(obj); } |
| 43 | + mutable T obj = 42; |
| 44 | +}; |
| 45 | + |
| 46 | +struct ExplicitConversionRvalueRef { |
| 47 | + operator int(); |
| 48 | + explicit operator int&&(); |
| 49 | +}; |
| 50 | + |
| 51 | +struct ExplicitConversionRef { |
| 52 | + operator int(); |
| 53 | + explicit operator int&(); |
| 54 | +}; |
| 55 | + |
| 56 | +template <typename T, typename U, bool Expected> |
| 57 | +constexpr void test_reference_constructs_from_temporary() { |
| 58 | + assert((std::reference_constructs_from_temporary<T, U>::value == Expected)); |
| 59 | + assert((std::reference_constructs_from_temporary_v<T, U> == Expected)); |
| 60 | +} |
| 61 | + |
| 62 | +constexpr bool test() { |
| 63 | + test_reference_constructs_from_temporary<int&, int&, false>(); |
| 64 | + test_reference_constructs_from_temporary<int&, int&, false>(); |
| 65 | + test_reference_constructs_from_temporary<int&, int&&, false>(); |
| 66 | + |
| 67 | + test_reference_constructs_from_temporary<const int&, int&, false>(); |
| 68 | + test_reference_constructs_from_temporary<const int&, const int&, false>(); |
| 69 | + test_reference_constructs_from_temporary<const int&, int&&, false>(); |
| 70 | + |
| 71 | + test_reference_constructs_from_temporary<int&, long&, false>(); // doesn't construct |
| 72 | + |
| 73 | + test_reference_constructs_from_temporary<const int&, long&, true>(); |
| 74 | + test_reference_constructs_from_temporary<const int&, long&&, true>(); |
| 75 | + test_reference_constructs_from_temporary<int&&, long&, true>(); |
| 76 | + |
| 77 | + using LRef = ConvertsToRef<int, int&>; |
| 78 | + using RRef = ConvertsToRef<int, int&&>; |
| 79 | + using CLRef = ConvertsToRef<int, const int&>; |
| 80 | + using LongRef = ConvertsToRef<long, long&>; |
| 81 | + |
| 82 | + assert((std::is_constructible_v<int&, LRef>)); |
| 83 | + test_reference_constructs_from_temporary<int&, LRef, false>(); |
| 84 | + |
| 85 | + assert((std::is_constructible_v<int&&, RRef>)); |
| 86 | + test_reference_constructs_from_temporary<int&&, RRef, false>(); |
| 87 | + |
| 88 | + assert((std::is_constructible_v<const int&, CLRef>)); |
| 89 | + test_reference_constructs_from_temporary<int&&, CLRef, false>(); |
| 90 | + |
| 91 | + assert((std::is_constructible_v<const int&, LongRef>)); |
| 92 | + test_reference_constructs_from_temporary<const int&, LongRef, true>(); |
| 93 | + test_reference_constructs_from_temporary<const int&, ConvertsToRefPrivate<long, long&>, false>(); |
| 94 | + |
| 95 | + // Test that it doesn't accept non-reference types as input. |
| 96 | + test_reference_constructs_from_temporary<int, long, false>(); |
| 97 | + |
| 98 | + test_reference_constructs_from_temporary<const int&, long, true>(); |
| 99 | + |
| 100 | + // Additional checks |
| 101 | + test_reference_constructs_from_temporary<POD const&, Derives, true>(); |
| 102 | + test_reference_constructs_from_temporary<int&&, int, true>(); |
| 103 | + test_reference_constructs_from_temporary<const int&, int, true>(); |
| 104 | + test_reference_constructs_from_temporary<int&&, int&&, false>(); |
| 105 | + test_reference_constructs_from_temporary<const int&, int&&, false>(); |
| 106 | + test_reference_constructs_from_temporary<int&&, long&&, true>(); |
| 107 | + test_reference_constructs_from_temporary<int&&, long, true>(); |
| 108 | + |
| 109 | + test_reference_constructs_from_temporary<int&, ExplicitConversionRef, false>(); |
| 110 | + test_reference_constructs_from_temporary<const int&, ExplicitConversionRef, false>(); |
| 111 | + test_reference_constructs_from_temporary<int&&, ExplicitConversionRvalueRef, false>(); |
| 112 | + |
| 113 | + return true; |
| 114 | +} |
| 115 | + |
| 116 | +int main(int, char**) { |
| 117 | + test(); |
| 118 | + static_assert(test()); |
| 119 | + |
| 120 | + return 0; |
| 121 | +} |
0 commit comments