15a83710eSEric Fiselier //===----------------------------------------------------------------------===//
25a83710eSEric Fiselier //
357b08b09SChandler Carruth // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
457b08b09SChandler Carruth // See https://llvm.org/LICENSE.txt for license information.
557b08b09SChandler Carruth // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
65a83710eSEric Fiselier //
75a83710eSEric Fiselier //===----------------------------------------------------------------------===//
85a83710eSEric Fiselier 
95a83710eSEric Fiselier // <functional>
105a83710eSEric Fiselier 
115a83710eSEric Fiselier // reference_wrapper
125a83710eSEric Fiselier 
135a83710eSEric Fiselier // template <ObjectType T> reference_wrapper<T> ref(reference_wrapper<T> t);
145a83710eSEric Fiselier 
155a83710eSEric Fiselier #include <functional>
165a83710eSEric Fiselier #include <cassert>
175a83710eSEric Fiselier 
18cc89063bSNico Weber #include "counting_predicates.h"
195a83710eSEric Fiselier 
207fc6a556SMarshall Clow #include "test_macros.h"
217fc6a556SMarshall Clow 
is5(int i)225a83710eSEric Fiselier bool is5 ( int i ) { return i == 5; }
235a83710eSEric Fiselier 
245a83710eSEric Fiselier template <typename T>
call_pred(T pred)255a83710eSEric Fiselier bool call_pred ( T pred ) { return pred(5); }
265a83710eSEric Fiselier 
27e442f383SLogan Smith namespace adl {
28e442f383SLogan Smith   struct A {};
ref(A)29e442f383SLogan Smith   void ref(A) {}
30e442f383SLogan Smith }
31e442f383SLogan Smith 
test()32*a13c1058SArthur O'Dwyer TEST_CONSTEXPR_CXX20 bool test()
335a83710eSEric Fiselier {
345a83710eSEric Fiselier   {
355a83710eSEric Fiselier     int i = 0;
365a83710eSEric Fiselier     std::reference_wrapper<int> r1 = std::ref(i);
375a83710eSEric Fiselier     std::reference_wrapper<int> r2 = std::ref(r1);
385a83710eSEric Fiselier     assert(&r2.get() == &i);
395a83710eSEric Fiselier   }
405a83710eSEric Fiselier   {
41e442f383SLogan Smith     adl::A a;
42e442f383SLogan Smith     std::reference_wrapper<adl::A> a1 = std::ref(a);
43e442f383SLogan Smith     std::reference_wrapper<adl::A> a2 = std::ref(a1);
44e442f383SLogan Smith     assert(&a2.get() == &a);
45e442f383SLogan Smith   }
46*a13c1058SArthur O'Dwyer   return true;
47*a13c1058SArthur O'Dwyer }
48*a13c1058SArthur O'Dwyer 
main(int,char **)49*a13c1058SArthur O'Dwyer int main(int, char**)
50*a13c1058SArthur O'Dwyer {
51*a13c1058SArthur O'Dwyer   test();
52*a13c1058SArthur O'Dwyer #if TEST_STD_VER > 17
53*a13c1058SArthur O'Dwyer   static_assert(test());
54*a13c1058SArthur O'Dwyer #endif
55e442f383SLogan Smith 
56e442f383SLogan Smith   {
575a83710eSEric Fiselier     unary_counting_predicate<bool(*)(int), int> cp(is5);
585a83710eSEric Fiselier     assert(!cp(6));
595a83710eSEric Fiselier     assert(cp.count() == 1);
605a83710eSEric Fiselier     assert(call_pred(cp));
615a83710eSEric Fiselier     assert(cp.count() == 1);
625a83710eSEric Fiselier     assert(call_pred(std::ref(cp)));
635a83710eSEric Fiselier     assert(cp.count() == 2);
645a83710eSEric Fiselier   }
652df59c50SJF Bastien 
662df59c50SJF Bastien   return 0;
675a83710eSEric Fiselier }
68