| 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 | |
| 20 | class functor1 |
| 21 | { |
| 22 | }; |
| 23 | |
| 24 | struct convertible_to_int_ref { |
| 25 | int val = 0; |
| 26 | operator int&() { return val; } |
| 27 | operator int const&() const { return val; } |
| 28 | }; |
| 29 | |
| 30 | template <class T> |
| 31 | void |
| 32 | test(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 | |
| 41 | void f() {} |
| 42 | void g() {} |
| 43 | |
| 44 | void |
| 45 | test_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 | |
| 53 | int 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 | |