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// <functional>
10
11// reference_wrapper
12
13// template <ObjectType T> reference_wrapper<T> ref(reference_wrapper<T> t);
14
15#include <functional>
16#include <cassert>
17
18#include "counting_predicates.h"
19
20#include "test_macros.h"
21
22bool is5 ( int i ) { return i == 5; }
23
24template <typename T>
25bool call_pred ( T pred ) { return pred(5); }
26
27namespace adl {
28 struct A {};
29 void ref(A) {}
30}
31
32TEST_CONSTEXPR_CXX20 bool test()
33{
34 {
35 int i = 0;
36 std::reference_wrapper<int> r1 = std::ref(t&: i);
37 std::reference_wrapper<int> r2 = std::ref(t: r1);
38 assert(&r2.get() == &i);
39 }
40 {
41 adl::A a;
42 std::reference_wrapper<adl::A> a1 = std::ref(t&: a);
43 std::reference_wrapper<adl::A> a2 = std::ref(t: a1);
44 assert(&a2.get() == &a);
45 }
46 return true;
47}
48
49int main(int, char**)
50{
51 test();
52#if TEST_STD_VER > 17
53 static_assert(test());
54#endif
55
56 {
57 unary_counting_predicate<bool(*)(int), int> cp(is5);
58 assert(!cp(6));
59 assert(cp.count() == 1);
60 assert(call_pred(cp));
61 assert(cp.count() == 1);
62 assert(call_pred(std::ref(cp)));
63 assert(cp.count() == 2);
64 }
65
66 return 0;
67}
68

source code of libcxx/test/std/utilities/function.objects/refwrap/refwrap.helpers/ref_2.pass.cpp