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// reference_wrapper& operator=(const reference_wrapper<T>& x);
14
15#include <functional>
16#include <cassert>
17
18#include "test_macros.h"
19
20class functor1
21{
22};
23
24struct convertible_to_int_ref {
25 int val = 0;
26 operator int&() { return val; }
27 operator int const&() const { return val; }
28};
29
30template <class T>
31void
32test(T& t)
33{
34 std::reference_wrapper<T> r(t);
35 T t2 = t;
36 std::reference_wrapper<T> r2(t2);
37 r2 = r;
38 assert(&r2.get() == &t);
39}
40
41void f() {}
42void g() {}
43
44void
45test_function()
46{
47 std::reference_wrapper<void ()> r(f);
48 std::reference_wrapper<void ()> r2(g);
49 r2 = r;
50 assert(&r2.get() == &f);
51}
52
53int main(int, char**)
54{
55 void (*fp)() = f;
56 test(t&: fp);
57 test_function();
58 functor1 f1;
59 test(t&: f1);
60 int i = 0;
61 test(t&: i);
62 const int j = 0;
63 test(t: j);
64
65#if TEST_STD_VER >= 11
66 convertible_to_int_ref convi;
67 test(convi);
68 convertible_to_int_ref const convic;
69 test(convic);
70
71 {
72 using Ref = std::reference_wrapper<int>;
73 static_assert((std::is_assignable<Ref&, int&>::value), "");
74 static_assert((!std::is_assignable<Ref&, int>::value), "");
75 static_assert((!std::is_assignable<Ref&, int&&>::value), "");
76 }
77#endif
78
79 return 0;
80}
81

source code of libcxx/test/std/utilities/function.objects/refwrap/refwrap.assign/copy_assign.pass.cpp