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
|
//===----------------------------------------------------------------------===//
//
// 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 F, class T> constexpr decltype(auto) apply(F &&, T &&)
// Test with different ref/ptr/cv qualified argument types.
#include <tuple>
#include <array>
#include <utility>
#include <cassert>
#include "test_macros.h"
#include "type_id.h"
// std::array is explicitly allowed to be initialized with A a = { init-list };.
// Disable the missing braces warning for this reason.
#include "disable_missing_braces_warning.h"
constexpr int constexpr_sum_fn() { return 0; }
template <class ...Ints>
constexpr int constexpr_sum_fn(int x1, Ints... rest) { return x1 + constexpr_sum_fn(rest...); }
struct ConstexprSumT {
constexpr ConstexprSumT() = default;
template <class ...Ints>
constexpr int operator()(Ints... values) const {
return constexpr_sum_fn(values...);
}
};
void test_constexpr_evaluation()
{
constexpr ConstexprSumT sum_obj{};
{
using Tup = std::tuple<>;
using Fn = int(&)();
constexpr Tup t;
static_assert(std::apply(static_cast<Fn>(constexpr_sum_fn), t) == 0, "");
static_assert(std::apply(sum_obj, t) == 0, "");
}
{
using Tup = std::tuple<int>;
using Fn = int(&)(int);
constexpr Tup t(42);
static_assert(std::apply(static_cast<Fn>(constexpr_sum_fn), t) == 42, "");
static_assert(std::apply(sum_obj, t) == 42, "");
}
{
using Tup = std::tuple<int, long>;
using Fn = int(&)(int, int);
constexpr Tup t(42, 101);
static_assert(std::apply(static_cast<Fn>(constexpr_sum_fn), t) == 143, "");
static_assert(std::apply(sum_obj, t) == 143, "");
}
{
using Tup = std::pair<int, long>;
using Fn = int(&)(int, int);
constexpr Tup t(42, 101);
static_assert(std::apply(static_cast<Fn>(constexpr_sum_fn), t) == 143, "");
static_assert(std::apply(sum_obj, t) == 143, "");
}
{
using Tup = std::tuple<int, long, int>;
using Fn = int(&)(int, int, int);
constexpr Tup t(42, 101, -1);
static_assert(std::apply(static_cast<Fn>(constexpr_sum_fn), t) == 142, "");
static_assert(std::apply(sum_obj, t) == 142, "");
}
{
using Tup = std::array<int, 3>;
using Fn = int(&)(int, int, int);
constexpr Tup t = {42, 101, -1};
static_assert(std::apply(static_cast<Fn>(constexpr_sum_fn), t) == 142, "");
static_assert(std::apply(sum_obj, t) == 142, "");
}
}
enum CallQuals {
CQ_None,
CQ_LValue,
CQ_ConstLValue,
CQ_RValue,
CQ_ConstRValue
};
template <class Tuple>
struct CallInfo {
CallQuals quals;
TypeID const* arg_types;
Tuple args;
template <class ...Args>
CallInfo(CallQuals q, Args&&... xargs)
: quals(q), arg_types(&makeArgumentID<Args&&...>()), args(std::forward<Args>(xargs)...)
{}
};
template <class ...Args>
inline CallInfo<decltype(std::forward_as_tuple(std::declval<Args>()...))>
makeCallInfo(CallQuals quals, Args&&... args) {
return {quals, std::forward<Args>(args)...};
}
struct TrackedCallable {
TrackedCallable() = default;
template <class ...Args> auto operator()(Args&&... xargs) &
{ return makeCallInfo(CQ_LValue, std::forward<Args>(xargs)...); }
template <class ...Args> auto operator()(Args&&... xargs) const&
{ return makeCallInfo(CQ_ConstLValue, std::forward<Args>(xargs)...); }
template <class ...Args> auto operator()(Args&&... xargs) &&
{ return makeCallInfo(CQ_RValue, std::forward<Args>(xargs)...); }
template <class ...Args> auto operator()(Args&&... xargs) const&&
{ return makeCallInfo(CQ_ConstRValue, std::forward<Args>(xargs)...); }
};
template <class ...ExpectArgs, class Tuple>
void check_apply_quals_and_types(Tuple&& t) {
TypeID const* const expect_args = &makeArgumentID<ExpectArgs...>();
TrackedCallable obj;
TrackedCallable const& cobj = obj;
{
auto ret = std::apply(obj, std::forward<Tuple>(t));
assert(ret.quals == CQ_LValue);
assert(ret.arg_types == expect_args);
assert(ret.args == t);
}
{
auto ret = std::apply(cobj, std::forward<Tuple>(t));
assert(ret.quals == CQ_ConstLValue);
assert(ret.arg_types == expect_args);
assert(ret.args == t);
}
{
auto ret = std::apply(std::move(obj), std::forward<Tuple>(t));
assert(ret.quals == CQ_RValue);
assert(ret.arg_types == expect_args);
assert(ret.args == t);
}
{
auto ret = std::apply(std::move(cobj), std::forward<Tuple>(t));
assert(ret.quals == CQ_ConstRValue);
assert(ret.arg_types == expect_args);
assert(ret.args == t);
}
}
void test_call_quals_and_arg_types()
{
using Tup = std::tuple<int, int const&, unsigned&&>;
const int x = 42;
unsigned y = 101;
Tup t(-1, x, std::move(y));
Tup const& ct = t;
check_apply_quals_and_types<int&, int const&, unsigned&>(t);
check_apply_quals_and_types<int const&, int const&, unsigned&>(ct);
check_apply_quals_and_types<int&&, int const&, unsigned&&>(std::move(t));
check_apply_quals_and_types<int const&&, int const&, unsigned&&>(std::move(ct));
}
struct NothrowMoveable {
NothrowMoveable() noexcept = default;
NothrowMoveable(NothrowMoveable const&) noexcept(false) {}
NothrowMoveable(NothrowMoveable&&) noexcept {}
};
template <bool IsNoexcept>
struct TestNoexceptCallable {
template <class ...Args>
NothrowMoveable operator()(Args...) const noexcept(IsNoexcept) { return {}; }
};
void test_noexcept()
{
TestNoexceptCallable<true> nec;
TestNoexceptCallable<false> tc;
{
// test that the functions noexcept-ness is propagated
using Tup = std::tuple<int, const char*, long>;
Tup t;
LIBCPP_ASSERT_NOEXCEPT(std::apply(nec, t));
ASSERT_NOT_NOEXCEPT(std::apply(tc, t));
}
{
// test that the noexcept-ness of the argument conversions is checked.
using Tup = std::tuple<NothrowMoveable, int>;
Tup t;
ASSERT_NOT_NOEXCEPT(std::apply(nec, t));
LIBCPP_ASSERT_NOEXCEPT(std::apply(nec, std::move(t)));
}
}
namespace ReturnTypeTest {
static int my_int = 42;
template <int N> struct index {};
void f(index<0>) {}
int f(index<1>) { return 0; }
int & f(index<2>) { return static_cast<int &>(my_int); }
int const & f(index<3>) { return static_cast<int const &>(my_int); }
int volatile & f(index<4>) { return static_cast<int volatile &>(my_int); }
int const volatile & f(index<5>) { return static_cast<int const volatile &>(my_int); }
int && f(index<6>) { return static_cast<int &&>(my_int); }
int const && f(index<7>) { return static_cast<int const &&>(my_int); }
int volatile && f(index<8>) { return static_cast<int volatile &&>(my_int); }
int const volatile && f(index<9>) { return static_cast<int const volatile &&>(my_int); }
int * f(index<10>) { return static_cast<int *>(&my_int); }
int const * f(index<11>) { return static_cast<int const *>(&my_int); }
int volatile * f(index<12>) { return static_cast<int volatile *>(&my_int); }
int const volatile * f(index<13>) { return static_cast<int const volatile *>(&my_int); }
template <int Func, class Expect>
void test()
{
using RawInvokeResult = decltype(f(index<Func>{}));
static_assert(std::is_same<RawInvokeResult, Expect>::value, "");
using FnType = RawInvokeResult (*) (index<Func>);
FnType fn = f;
std::tuple<index<Func>> t; ((void)t);
using InvokeResult = decltype(std::apply(fn, t));
static_assert(std::is_same<InvokeResult, Expect>::value, "");
}
} // end namespace ReturnTypeTest
void test_return_type()
{
using ReturnTypeTest::test;
test<0, void>();
test<1, int>();
test<2, int &>();
test<3, int const &>();
test<4, int volatile &>();
test<5, int const volatile &>();
test<6, int &&>();
test<7, int const &&>();
test<8, int volatile &&>();
test<9, int const volatile &&>();
test<10, int *>();
test<11, int const *>();
test<12, int volatile *>();
test<13, int const volatile *>();
}
int main(int, char**) {
test_constexpr_evaluation();
test_call_quals_and_arg_types();
test_return_type();
test_noexcept();
return 0;
}
|