| 1 | // Copyright Louis Dionne 2013-2022 |
| 2 | // Distributed under the Boost Software License, Version 1.0. |
| 3 | // (See accompanying file LICENSE.md or copy at http://boost.org/LICENSE_1_0.txt) |
| 4 | |
| 5 | #include <boost/hana/assert.hpp> |
| 6 | #include <boost/hana/equal.hpp> |
| 7 | #include <boost/hana/type.hpp> |
| 8 | namespace hana = boost::hana; |
| 9 | |
| 10 | |
| 11 | using Function = void(); |
| 12 | void function() { } |
| 13 | |
| 14 | int main() { |
| 15 | { |
| 16 | struct T { }; |
| 17 | T t; |
| 18 | |
| 19 | BOOST_HANA_CONSTANT_CHECK(hana::equal( |
| 20 | hana::decltype_(T{}), |
| 21 | hana::type_c<T> |
| 22 | )); |
| 23 | |
| 24 | BOOST_HANA_CONSTANT_CHECK(hana::equal( |
| 25 | hana::decltype_(t), |
| 26 | hana::type_c<T> |
| 27 | )); |
| 28 | } |
| 29 | |
| 30 | // [cv-qualified] reference types |
| 31 | { |
| 32 | struct T { }; |
| 33 | T t; |
| 34 | |
| 35 | BOOST_HANA_CONSTANT_CHECK(hana::equal( |
| 36 | hana::decltype_(static_cast<T&>(t)), |
| 37 | hana::type_c<T> |
| 38 | )); |
| 39 | |
| 40 | BOOST_HANA_CONSTANT_CHECK(hana::equal( |
| 41 | hana::decltype_(static_cast<T const&>(t)), |
| 42 | hana::type_c<T const> |
| 43 | )); |
| 44 | |
| 45 | BOOST_HANA_CONSTANT_CHECK(hana::equal( |
| 46 | hana::decltype_(static_cast<T volatile&>(t)), |
| 47 | hana::type_c<T volatile> |
| 48 | )); |
| 49 | |
| 50 | BOOST_HANA_CONSTANT_CHECK(hana::equal( |
| 51 | hana::decltype_(static_cast<T const volatile&>(t)), |
| 52 | hana::type_c<T const volatile> |
| 53 | )); |
| 54 | } |
| 55 | |
| 56 | // [cv-qualified] rvalue reference types |
| 57 | { |
| 58 | struct T { }; |
| 59 | T t; |
| 60 | |
| 61 | BOOST_HANA_CONSTANT_CHECK(hana::equal( |
| 62 | hana::decltype_(static_cast<T&&>(t)), |
| 63 | hana::type_c<T> |
| 64 | )); |
| 65 | |
| 66 | BOOST_HANA_CONSTANT_CHECK(hana::equal( |
| 67 | hana::decltype_(static_cast<T const &&>(t)), |
| 68 | hana::type_c<T const> |
| 69 | )); |
| 70 | |
| 71 | BOOST_HANA_CONSTANT_CHECK(hana::equal( |
| 72 | hana::decltype_(static_cast<T volatile&&>(t)), |
| 73 | hana::type_c<T volatile> |
| 74 | )); |
| 75 | |
| 76 | BOOST_HANA_CONSTANT_CHECK(hana::equal( |
| 77 | hana::decltype_(static_cast<T const volatile&&>(t)), |
| 78 | hana::type_c<T const volatile> |
| 79 | )); |
| 80 | } |
| 81 | |
| 82 | // decltype_(type_c<T>) is the identity function |
| 83 | { |
| 84 | struct T; |
| 85 | auto const type_const = hana::type_c<T>; |
| 86 | auto const& type_const_ref = hana::type_c<T>; |
| 87 | auto& type_ref = hana::type_c<T>; |
| 88 | auto&& type_ref_ref = static_cast<decltype(type_ref)&&>(type_ref); |
| 89 | |
| 90 | BOOST_HANA_CONSTANT_CHECK(hana::equal( |
| 91 | hana::decltype_(hana::type_c<T>), |
| 92 | hana::type_c<T> |
| 93 | )); |
| 94 | |
| 95 | BOOST_HANA_CONSTANT_CHECK(hana::equal( |
| 96 | hana::decltype_(type_const), |
| 97 | hana::type_c<T> |
| 98 | )); |
| 99 | |
| 100 | BOOST_HANA_CONSTANT_CHECK(hana::equal( |
| 101 | hana::decltype_(type_const_ref), |
| 102 | hana::type_c<T> |
| 103 | )); |
| 104 | |
| 105 | BOOST_HANA_CONSTANT_CHECK(hana::equal( |
| 106 | hana::decltype_(type_ref), |
| 107 | hana::type_c<T> |
| 108 | )); |
| 109 | |
| 110 | BOOST_HANA_CONSTANT_CHECK(hana::equal( |
| 111 | hana::decltype_(type_ref_ref), |
| 112 | hana::type_c<T> |
| 113 | )); |
| 114 | } |
| 115 | |
| 116 | // make sure we don't read from non-constexpr variables |
| 117 | { |
| 118 | struct T; |
| 119 | auto t = hana::type_c<T>; |
| 120 | auto x = 1; |
| 121 | constexpr auto r1 = hana::decltype_(t); (void)r1; |
| 122 | constexpr auto r2 = hana::decltype_(x); (void)r2; |
| 123 | } |
| 124 | |
| 125 | // decltype_ with builtin arrays, function pointers and other weirdos |
| 126 | { |
| 127 | struct T { }; |
| 128 | using A = T[3]; |
| 129 | A a; |
| 130 | A& a_ref = a; |
| 131 | A const& a_const_ref = a; |
| 132 | |
| 133 | BOOST_HANA_CONSTANT_CHECK(hana::equal( |
| 134 | hana::decltype_(a), |
| 135 | hana::type_c<A> |
| 136 | )); |
| 137 | |
| 138 | BOOST_HANA_CONSTANT_CHECK(hana::equal( |
| 139 | hana::decltype_(a_ref), |
| 140 | hana::type_c<A> |
| 141 | )); |
| 142 | |
| 143 | BOOST_HANA_CONSTANT_CHECK(hana::equal( |
| 144 | hana::decltype_(a_const_ref), |
| 145 | hana::type_c<A const> |
| 146 | )); |
| 147 | } |
| 148 | { |
| 149 | using Fptr = int(*)(); |
| 150 | Fptr f; |
| 151 | Fptr& f_ref = f; |
| 152 | Fptr const& f_const_ref = f; |
| 153 | |
| 154 | BOOST_HANA_CONSTANT_CHECK(hana::equal( |
| 155 | hana::decltype_(f), |
| 156 | hana::type_c<Fptr> |
| 157 | )); |
| 158 | |
| 159 | BOOST_HANA_CONSTANT_CHECK(hana::equal( |
| 160 | hana::decltype_(f_ref), |
| 161 | hana::type_c<Fptr> |
| 162 | )); |
| 163 | |
| 164 | BOOST_HANA_CONSTANT_CHECK(hana::equal( |
| 165 | hana::decltype_(f_const_ref), |
| 166 | hana::type_c<Fptr const> |
| 167 | )); |
| 168 | } |
| 169 | { |
| 170 | Function& function_ref = function; |
| 171 | |
| 172 | BOOST_HANA_CONSTANT_CHECK(hana::equal( |
| 173 | hana::decltype_(function), |
| 174 | hana::type_c<Function> |
| 175 | )); |
| 176 | |
| 177 | BOOST_HANA_CONSTANT_CHECK(hana::equal( |
| 178 | hana::decltype_(function_ref), |
| 179 | hana::type_c<Function> |
| 180 | )); |
| 181 | } |
| 182 | } |
| 183 | |