1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295
|
//===----------------------------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
// UNSUPPORTED: c++03, c++11, c++14
// <tuple>
// template <class T, class Tuple> constexpr T make_from_tuple(Tuple&&);
#include <tuple>
#include <array>
#include <utility>
#include <string>
#include <cassert>
#include "test_macros.h"
#include "type_id.h"
template <class Tuple>
struct ConstexprConstructibleFromTuple {
template <class ...Args>
explicit constexpr ConstexprConstructibleFromTuple(Args&&... xargs)
: args{std::forward<Args>(xargs)...} {}
Tuple args;
};
template <class TupleLike>
struct ConstructibleFromTuple;
template <template <class ...> class Tuple, class ...Types>
struct ConstructibleFromTuple<Tuple<Types...>> {
template <class ...Args>
explicit ConstructibleFromTuple(Args&&... xargs)
: args(xargs...),
arg_types(&makeArgumentID<Args&&...>())
{}
Tuple<std::decay_t<Types>...> args;
TypeID const* arg_types;
};
template <class Tp, std::size_t N>
struct ConstructibleFromTuple<std::array<Tp, N>> {
template <class ...Args>
explicit ConstructibleFromTuple(Args&&... xargs)
: args{xargs...},
arg_types(&makeArgumentID<Args&&...>())
{}
std::array<Tp, N> args;
TypeID const* arg_types;
};
template <class Tuple>
constexpr bool do_constexpr_test(Tuple&& tup) {
using RawTuple = std::decay_t<Tuple>;
using Tp = ConstexprConstructibleFromTuple<RawTuple>;
return std::make_from_tuple<Tp>(std::forward<Tuple>(tup)).args == tup;
}
template <class ...ExpectTypes, class Tuple>
bool do_forwarding_test(Tuple&& tup) {
using RawTuple = std::decay_t<Tuple>;
using Tp = ConstructibleFromTuple<RawTuple>;
const Tp value = std::make_from_tuple<Tp>(std::forward<Tuple>(tup));
return value.args == tup
&& value.arg_types == &makeArgumentID<ExpectTypes...>();
}
void test_constexpr_construction() {
{
constexpr std::tuple<> tup;
static_assert(do_constexpr_test(tup), "");
}
{
constexpr std::tuple<int> tup(42);
static_assert(do_constexpr_test(tup), "");
}
{
constexpr std::tuple<int, long, void*> tup(42, 101, nullptr);
static_assert(do_constexpr_test(tup), "");
}
{
constexpr std::pair<int, const char*> p(42, "hello world");
static_assert(do_constexpr_test(p), "");
}
{
using Tuple = std::array<int, 3>;
using ValueTp = ConstexprConstructibleFromTuple<Tuple>;
constexpr Tuple arr = {42, 101, -1};
constexpr ValueTp value = std::make_from_tuple<ValueTp>(arr);
static_assert(value.args[0] == arr[0] && value.args[1] == arr[1]
&& value.args[2] == arr[2], "");
}
}
void test_perfect_forwarding() {
{
using Tup = std::tuple<>;
Tup tup;
Tup const& ctup = tup;
assert(do_forwarding_test<>(tup));
assert(do_forwarding_test<>(ctup));
}
{
using Tup = std::tuple<int>;
Tup tup(42);
Tup const& ctup = tup;
assert(do_forwarding_test<int&>(tup));
assert(do_forwarding_test<int const&>(ctup));
assert(do_forwarding_test<int&&>(std::move(tup)));
assert(do_forwarding_test<int const&&>(std::move(ctup)));
}
{
using Tup = std::tuple<int&, const char*, unsigned&&>;
int x = 42;
unsigned y = 101;
Tup tup(x, "hello world", std::move(y));
Tup const& ctup = tup;
assert((do_forwarding_test<int&, const char*&, unsigned&>(tup)));
assert((do_forwarding_test<int&, const char* const&, unsigned &>(ctup)));
assert((do_forwarding_test<int&, const char*&&, unsigned&&>(std::move(tup))));
assert((do_forwarding_test<int&, const char* const&&, unsigned &&>(std::move(ctup))));
}
// test with pair<T, U>
{
using Tup = std::pair<int&, const char*>;
int x = 42;
Tup tup(x, "hello world");
Tup const& ctup = tup;
assert((do_forwarding_test<int&, const char*&>(tup)));
assert((do_forwarding_test<int&, const char* const&>(ctup)));
assert((do_forwarding_test<int&, const char*&&>(std::move(tup))));
assert((do_forwarding_test<int&, const char* const&&>(std::move(ctup))));
}
// test with array<T, I>
{
using Tup = std::array<int, 3>;
Tup tup = {42, 101, -1};
Tup const& ctup = tup;
assert((do_forwarding_test<int&, int&, int&>(tup)));
assert((do_forwarding_test<int const&, int const&, int const&>(ctup)));
assert((do_forwarding_test<int&&, int&&, int&&>(std::move(tup))));
assert((do_forwarding_test<int const&&, int const&&, int const&&>(std::move(ctup))));
}
}
void test_noexcept() {
struct NothrowMoveable {
NothrowMoveable() = default;
NothrowMoveable(NothrowMoveable const&) {}
NothrowMoveable(NothrowMoveable&&) noexcept {}
};
struct TestType {
TestType(int, NothrowMoveable) noexcept {}
TestType(int, int, int) noexcept(false) {}
TestType(long, long, long) noexcept {}
};
{
using Tuple = std::tuple<int, NothrowMoveable>;
Tuple tup; ((void)tup);
Tuple const& ctup = tup; ((void)ctup);
ASSERT_NOT_NOEXCEPT(std::make_from_tuple<TestType>(ctup));
LIBCPP_ASSERT_NOEXCEPT(std::make_from_tuple<TestType>(std::move(tup)));
}
{
using Tuple = std::pair<int, NothrowMoveable>;
Tuple tup; ((void)tup);
Tuple const& ctup = tup; ((void)ctup);
ASSERT_NOT_NOEXCEPT(std::make_from_tuple<TestType>(ctup));
LIBCPP_ASSERT_NOEXCEPT(std::make_from_tuple<TestType>(std::move(tup)));
}
{
using Tuple = std::tuple<int, int, int>;
Tuple tup; ((void)tup);
ASSERT_NOT_NOEXCEPT(std::make_from_tuple<TestType>(tup));
}
{
using Tuple = std::tuple<long, long, long>;
Tuple tup; ((void)tup);
LIBCPP_ASSERT_NOEXCEPT(std::make_from_tuple<TestType>(tup));
}
{
using Tuple = std::array<int, 3>;
Tuple tup; ((void)tup);
ASSERT_NOT_NOEXCEPT(std::make_from_tuple<TestType>(tup));
}
{
using Tuple = std::array<long, 3>;
Tuple tup; ((void)tup);
LIBCPP_ASSERT_NOEXCEPT(std::make_from_tuple<TestType>(tup));
}
}
namespace LWG3528 {
template <class T, class Tuple>
auto test_make_from_tuple(T&&, Tuple&& t) -> decltype(std::make_from_tuple<T>(t), uint8_t()) {
return 0;
}
template <class T, class Tuple>
uint32_t test_make_from_tuple(...) {
return 0;
}
template <class T, class Tuple>
static constexpr bool can_make_from_tuple =
std::is_same_v<decltype(test_make_from_tuple<T, Tuple>(T{}, Tuple{})), uint8_t>;
#ifdef _LIBCPP_VERSION
template <class T, class Tuple>
auto test_make_from_tuple_impl(T&&, Tuple&& t)
-> decltype(std::__make_from_tuple_impl<T>(
t, typename std::__make_tuple_indices< std::tuple_size_v<std::remove_reference_t<Tuple>>>::type{}),
uint8_t()) {
return 0;
}
template <class T, class Tuple>
uint32_t test_make_from_tuple_impl(...) {
return 0;
}
template <class T, class Tuple>
static constexpr bool can_make_from_tuple_impl =
std::is_same_v<decltype(test_make_from_tuple_impl<T, Tuple>(T{}, Tuple{})), uint8_t>;
#endif // _LIBCPP_VERSION
struct A {
int a;
};
struct B : public A {};
struct C {
C(const B&) {}
};
enum class D {
ONE,
TWO,
};
// Test std::make_from_tuple constraints.
// reinterpret_cast
static_assert(!can_make_from_tuple<int*, std::tuple<A*>>);
static_assert(can_make_from_tuple<A*, std::tuple<A*>>);
// const_cast
static_assert(!can_make_from_tuple<char*, std::tuple<const char*>>);
static_assert(!can_make_from_tuple<volatile char*, std::tuple<const volatile char*>>);
static_assert(can_make_from_tuple<volatile char*, std::tuple<volatile char*>>);
static_assert(can_make_from_tuple<char*, std::tuple<char*>>);
static_assert(can_make_from_tuple<const char*, std::tuple<char*>>);
static_assert(can_make_from_tuple<const volatile char*, std::tuple<volatile char*>>);
// static_cast
static_assert(!can_make_from_tuple<int, std::tuple<D>>);
static_assert(!can_make_from_tuple<D, std::tuple<int>>);
static_assert(can_make_from_tuple<long, std::tuple<int>>);
static_assert(can_make_from_tuple<double, std::tuple<float>>);
static_assert(can_make_from_tuple<float, std::tuple<double>>);
// Test std::__make_from_tuple_impl constraints.
// reinterpret_cast
LIBCPP_STATIC_ASSERT(!can_make_from_tuple_impl<int*, std::tuple<A*>>);
LIBCPP_STATIC_ASSERT(can_make_from_tuple_impl<A*, std::tuple<A*>>);
// const_cast
LIBCPP_STATIC_ASSERT(!can_make_from_tuple_impl<char*, std::tuple<const char*>>);
LIBCPP_STATIC_ASSERT(!can_make_from_tuple_impl<volatile char*, std::tuple<const volatile char*>>);
LIBCPP_STATIC_ASSERT(can_make_from_tuple_impl<volatile char*, std::tuple<volatile char*>>);
LIBCPP_STATIC_ASSERT(can_make_from_tuple_impl<char*, std::tuple<char*>>);
LIBCPP_STATIC_ASSERT(can_make_from_tuple_impl<const char*, std::tuple<char*>>);
LIBCPP_STATIC_ASSERT(can_make_from_tuple_impl<const volatile char*, std::tuple<volatile char*>>);
// static_cast
LIBCPP_STATIC_ASSERT(!can_make_from_tuple_impl<int, std::tuple<D>>);
LIBCPP_STATIC_ASSERT(!can_make_from_tuple_impl<D, std::tuple<int>>);
LIBCPP_STATIC_ASSERT(can_make_from_tuple_impl<long, std::tuple<int>>);
LIBCPP_STATIC_ASSERT(can_make_from_tuple_impl<double, std::tuple<float>>);
LIBCPP_STATIC_ASSERT(can_make_from_tuple_impl<float, std::tuple<double>>);
} // namespace LWG3528
int main(int, char**)
{
test_constexpr_construction();
test_perfect_forwarding();
test_noexcept();
return 0;
}
|