File: bind_front.pass.cpp

package info (click to toggle)
llvm-toolchain-13 1%3A13.0.1-6~deb11u1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 1,418,812 kB
  • sloc: cpp: 5,290,827; ansic: 996,570; asm: 544,593; python: 188,212; objc: 72,027; lisp: 30,291; f90: 25,395; sh: 24,900; javascript: 9,780; pascal: 9,398; perl: 7,484; ml: 5,432; awk: 3,523; makefile: 2,892; xml: 953; cs: 573; fortran: 539
file content (302 lines) | stat: -rw-r--r-- 8,087 bytes parent folder | download | duplicates (3)
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
296
297
298
299
300
301
302
//===----------------------------------------------------------------------===//
//
// 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, c++17

// functional

// template <class F, class... Args> constexpr unspecified bind_front(F&&, Args&&...);

#include <functional>

#include "callable_types.h"
#include "test_macros.h"

constexpr int add(int a, int b) { return a + b; }

constexpr int long_test(int a, int b, int c, int d, int e, int f) {
  return a + b + c + d + e + f;
}

struct Foo {
  int a;
  int b;
};

struct FooCall {
  constexpr Foo operator()(int a, int b) { return Foo{a, b}; }
};

struct S {
  constexpr bool operator()(int a) { return a == 1; }
};

struct CopyMoveInfo {
  enum { none, copy, move } copy_kind;

  constexpr CopyMoveInfo() : copy_kind(none) {}
  constexpr CopyMoveInfo(CopyMoveInfo const&) : copy_kind(copy) {}
  constexpr CopyMoveInfo(CopyMoveInfo&&) : copy_kind(move) {}
};

constexpr bool wasCopied(CopyMoveInfo info) {
  return info.copy_kind == CopyMoveInfo::copy;
}
constexpr bool wasMoved(CopyMoveInfo info) {
  return info.copy_kind == CopyMoveInfo::move;
}

constexpr void basic_tests() {
  int n = 2;
  int m = 1;

  auto a = std::bind_front(add, m, n);
  assert(a() == 3);

  auto b = std::bind_front(long_test, m, n, m, m, m, m);
  assert(b() == 7);

  auto c = std::bind_front(long_test, n, m);
  assert(c(1, 1, 1, 1) == 7);

  auto d = std::bind_front(S{}, m);
  assert(d());

  auto f = std::bind_front(add, n);
  assert(f(3) == 5);

  auto g = std::bind_front(add, n, 1);
  assert(g() == 3);

  auto h = std::bind_front(long_test, 1, 1, 1);
  assert(h(2, 2, 2) == 9);

  // Make sure the arg is passed by value.
  auto i = std::bind_front(add, n, 1);
  n = 100;
  assert(i() == 3);

  CopyMoveInfo info;
  auto copied = std::bind_front(wasCopied, info);
  assert(copied());

  auto moved = std::bind_front(wasMoved, info);
  assert(std::move(moved)());
}

struct variadic_fn {
  template <class... Args>
  constexpr int operator()(Args&&... args) {
    return sizeof...(args);
  }
};

constexpr void test_variadic() {
  variadic_fn value;
  auto fn = std::bind_front(value, 0, 0, 0);
  assert(fn(0, 0, 0) == 6);
}

struct mutable_callable {
  bool should_call_const;

  constexpr bool operator()(int, int) {
    assert(!should_call_const);
    return true;
  }
  constexpr bool operator()(int, int) const {
    assert(should_call_const);
    return true;
  }
};

constexpr void test_mutable() {
  const mutable_callable v1{true};
  const auto fn1 = std::bind_front(v1, 0);
  assert(fn1(0));

  mutable_callable v2{false};
  auto fn2 = std::bind_front(v2, 0);
  assert(fn2(0));
};

struct call_member {
  constexpr bool member(int, int) { return true; }
};

constexpr void test_call_member() {
  call_member value;
  auto fn = std::bind_front(&call_member::member, value, 0);
  assert(fn(0));
}

struct no_const_lvalue {
  constexpr void operator()(int) && {};
};

constexpr auto make_no_const_lvalue(int x) {
  // This is to test that bind_front works when something like the following would not:
  // return [nc = no_const_lvalue{}, x] { return nc(x); };
  // Above would not work because it would look for a () const & overload.
  return std::bind_front(no_const_lvalue{}, x);
}

constexpr void test_no_const_lvalue() { make_no_const_lvalue(1)(); }

constexpr void constructor_tests() {
  {
    MoveOnlyCallable value(true);
    using RetT = decltype(std::bind_front(std::move(value), 1));

    static_assert(std::is_move_constructible<RetT>::value);
    static_assert(!std::is_copy_constructible<RetT>::value);
    static_assert(!std::is_move_assignable<RetT>::value);
    static_assert(!std::is_copy_assignable<RetT>::value);

    auto ret = std::bind_front(std::move(value), 1);
    assert(ret());
    assert(ret(1, 2, 3));

    auto ret1 = std::move(ret);
    assert(!ret());
    assert(ret1());
    assert(ret1(1, 2, 3));
  }
  {
    CopyCallable value(true);
    using RetT = decltype(std::bind_front(value, 1));

    static_assert(std::is_move_constructible<RetT>::value);
    static_assert(std::is_copy_constructible<RetT>::value);
    static_assert(!std::is_move_assignable<RetT>::value);
    static_assert(!std::is_copy_assignable<RetT>::value);

    auto ret = std::bind_front(value, 1);
    assert(ret());
    assert(ret(1, 2, 3));

    auto ret1 = std::move(ret);
    assert(ret1());
    assert(ret1(1, 2, 3));

    auto ret2 = std::bind_front(std::move(value), 1);
    assert(!ret());
    assert(ret2());
    assert(ret2(1, 2, 3));
  }
  {
    CopyAssignableWrapper value(true);
    using RetT = decltype(std::bind_front(value, 1));

    static_assert(std::is_move_constructible<RetT>::value);
    static_assert(std::is_copy_constructible<RetT>::value);
    static_assert(std::is_move_assignable<RetT>::value);
    static_assert(std::is_copy_assignable<RetT>::value);
  }
  {
    MoveAssignableWrapper value(true);
    using RetT = decltype(std::bind_front(std::move(value), 1));

    static_assert(std::is_move_constructible<RetT>::value);
    static_assert(!std::is_copy_constructible<RetT>::value);
    static_assert(std::is_move_assignable<RetT>::value);
    static_assert(!std::is_copy_assignable<RetT>::value);
  }
}

template <class Res, class F, class... Args>
constexpr void test_return(F&& value, Args&&... args) {
  auto ret =
      std::bind_front(std::forward<F>(value), std::forward<Args>(args)...);
  static_assert(std::is_same<decltype(ret()), Res>::value);
}

constexpr void test_return_types() {
  test_return<Foo>(FooCall{}, 1, 2);
  test_return<bool>(S{}, 1);
  test_return<int>(add, 2, 2);
}

constexpr void test_arg_count() {
  using T = decltype(std::bind_front(add, 1));
  static_assert(!std::is_invocable<T>::value);
  static_assert(std::is_invocable<T, int>::value);
}

template <class... Args>
struct is_bind_frontable {
  template <class... LocalArgs>
  static auto test(int)
      -> decltype((void)std::bind_front(std::declval<LocalArgs>()...),
                  std::true_type());

  template <class...>
  static std::false_type test(...);

  static constexpr bool value = decltype(test<Args...>(0))::value;
};

struct NotCopyMove {
  NotCopyMove() = delete;
  NotCopyMove(const NotCopyMove&) = delete;
  NotCopyMove(NotCopyMove&&) = delete;
  void operator()() {}
};

struct NonConstCopyConstructible {
  explicit NonConstCopyConstructible() {}
  NonConstCopyConstructible(NonConstCopyConstructible&) {}
};

struct MoveConstructible {
  explicit MoveConstructible() {}
  MoveConstructible(MoveConstructible&&) {}
};

constexpr void test_invocability() {
  static_assert(!std::is_constructible_v<NotCopyMove, NotCopyMove>);
  static_assert(!std::is_move_constructible_v<NotCopyMove>);
  static_assert(!is_bind_frontable<NotCopyMove>::value);
  static_assert(!is_bind_frontable<NotCopyMove&>::value);

  static_assert(
      !std::is_constructible_v<MoveConstructible, MoveConstructible&>);
  static_assert(std::is_move_constructible_v<MoveConstructible>);
  static_assert(is_bind_frontable<variadic_fn, MoveConstructible>::value);
  static_assert(
      !is_bind_frontable<variadic_fn, MoveConstructible&>::value);

  static_assert(std::is_constructible_v<NonConstCopyConstructible,
                                        NonConstCopyConstructible&>);
  static_assert(!std::is_move_constructible_v<NonConstCopyConstructible>);
  static_assert(
      !is_bind_frontable<variadic_fn, NonConstCopyConstructible&>::value);
  static_assert(
      !is_bind_frontable<variadic_fn, NonConstCopyConstructible>::value);
}

constexpr bool test() {
  basic_tests();
  constructor_tests();
  test_return_types();
  test_arg_count();
  test_variadic();
  test_mutable();
  test_call_member();
  test_no_const_lvalue();
  test_invocability();

  return true;
}

int main(int, char**) {
  test();
  static_assert(test());

  return 0;
}