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 // class function<R(ArgTypes...)>
125a83710eSEric Fiselier 
1388558e22SEric Fiselier // function(F);
145a83710eSEric Fiselier 
15b4fb705eSLouis Dionne // This test runs in C++03, but we have deprecated using std::function in C++03.
16*c475e31aSNikolas Klauser // ADDITIONAL_COMPILE_FLAGS: -D_LIBCPP_DISABLE_DEPRECATION_WARNINGS -D_LIBCPP_ENABLE_CXX03_FUNCTION
17b4fb705eSLouis Dionne 
185a83710eSEric Fiselier #include <functional>
195a83710eSEric Fiselier #include <cassert>
205a83710eSEric Fiselier 
21790df145SEric Fiselier #include "test_macros.h"
22cc89063bSNico Weber #include "count_new.h"
235a83710eSEric Fiselier 
245a83710eSEric Fiselier class A
255a83710eSEric Fiselier {
265a83710eSEric Fiselier     int data_[10];
275a83710eSEric Fiselier public:
285a83710eSEric Fiselier     static int count;
295a83710eSEric Fiselier 
A()305a83710eSEric Fiselier     A()
315a83710eSEric Fiselier     {
325a83710eSEric Fiselier         ++count;
335a83710eSEric Fiselier         for (int i = 0; i < 10; ++i)
345a83710eSEric Fiselier             data_[i] = i;
355a83710eSEric Fiselier     }
365a83710eSEric Fiselier 
A(const A &)375a83710eSEric Fiselier     A(const A&) {++count;}
385a83710eSEric Fiselier 
~A()395a83710eSEric Fiselier     ~A() {--count;}
405a83710eSEric Fiselier 
operator ()(int i) const415a83710eSEric Fiselier     int operator()(int i) const
425a83710eSEric Fiselier     {
435a83710eSEric Fiselier         for (int j = 0; j < 10; ++j)
445a83710eSEric Fiselier             i += data_[j];
455a83710eSEric Fiselier         return i;
465a83710eSEric Fiselier     }
475a83710eSEric Fiselier 
foo(int) const485a83710eSEric Fiselier     int foo(int) const {return 1;}
495a83710eSEric Fiselier };
505a83710eSEric Fiselier 
515a83710eSEric Fiselier int A::count = 0;
525a83710eSEric Fiselier 
g(int)535a83710eSEric Fiselier int g(int) {return 0;}
545a83710eSEric Fiselier 
55790df145SEric Fiselier #if TEST_STD_VER >= 11
56790df145SEric Fiselier struct RValueCallable {
57790df145SEric Fiselier     template <class ...Args>
operator ()RValueCallable58790df145SEric Fiselier     void operator()(Args&&...) && {}
59790df145SEric Fiselier };
60790df145SEric Fiselier struct LValueCallable {
61790df145SEric Fiselier     template <class ...Args>
operator ()LValueCallable62790df145SEric Fiselier     void operator()(Args&&...) & {}
63790df145SEric Fiselier };
64790df145SEric Fiselier #endif
65790df145SEric Fiselier 
main(int,char **)662df59c50SJF Bastien int main(int, char**)
675a83710eSEric Fiselier {
689c5d0ea6SDan Albert     globalMemCounter.reset();
692cbc654dSEric Fiselier     assert(globalMemCounter.checkOutstandingNewEq(0));
705a83710eSEric Fiselier     {
715a83710eSEric Fiselier     std::function<int(int)> f = A();
725a83710eSEric Fiselier     assert(A::count == 1);
732cbc654dSEric Fiselier     assert(globalMemCounter.checkOutstandingNewEq(1));
74bb09ef95SLouis Dionne     RTTI_ASSERT(f.target<A>());
75bb09ef95SLouis Dionne     RTTI_ASSERT(f.target<int(*)(int)>() == 0);
765a83710eSEric Fiselier     }
775a83710eSEric Fiselier     assert(A::count == 0);
782cbc654dSEric Fiselier     assert(globalMemCounter.checkOutstandingNewEq(0));
795a83710eSEric Fiselier     {
805a83710eSEric Fiselier     std::function<int(int)> f = g;
812cbc654dSEric Fiselier     assert(globalMemCounter.checkOutstandingNewEq(0));
82bb09ef95SLouis Dionne     RTTI_ASSERT(f.target<int(*)(int)>());
83bb09ef95SLouis Dionne     RTTI_ASSERT(f.target<A>() == 0);
845a83710eSEric Fiselier     }
852cbc654dSEric Fiselier     assert(globalMemCounter.checkOutstandingNewEq(0));
865a83710eSEric Fiselier     {
875a83710eSEric Fiselier     std::function<int(int)> f = (int (*)(int))0;
885a83710eSEric Fiselier     assert(!f);
892cbc654dSEric Fiselier     assert(globalMemCounter.checkOutstandingNewEq(0));
90bb09ef95SLouis Dionne     RTTI_ASSERT(f.target<int(*)(int)>() == 0);
91bb09ef95SLouis Dionne     RTTI_ASSERT(f.target<A>() == 0);
925a83710eSEric Fiselier     }
935a83710eSEric Fiselier     {
945a83710eSEric Fiselier     std::function<int(const A*, int)> f = &A::foo;
955a83710eSEric Fiselier     assert(f);
962cbc654dSEric Fiselier     assert(globalMemCounter.checkOutstandingNewEq(0));
97bb09ef95SLouis Dionne     RTTI_ASSERT(f.target<int (A::*)(int) const>() != 0);
985a83710eSEric Fiselier     }
9954519a6bSEric Fiselier     {
10054519a6bSEric Fiselier       std::function<void(int)> f(&g);
10154519a6bSEric Fiselier       assert(f);
102bb09ef95SLouis Dionne       RTTI_ASSERT(f.target<int(*)(int)>() != 0);
10354519a6bSEric Fiselier       f(1);
10454519a6bSEric Fiselier     }
10588558e22SEric Fiselier     {
10688558e22SEric Fiselier         std::function <void()> f(static_cast<void (*)()>(0));
10788558e22SEric Fiselier         assert(!f);
10888558e22SEric Fiselier     }
109790df145SEric Fiselier #if TEST_STD_VER >= 11
110790df145SEric Fiselier     {
111790df145SEric Fiselier         using Fn = std::function<void(int, int, int)>;
112790df145SEric Fiselier         static_assert(std::is_constructible<Fn, LValueCallable&>::value, "");
113790df145SEric Fiselier         static_assert(std::is_constructible<Fn, LValueCallable>::value, "");
114790df145SEric Fiselier         static_assert(!std::is_constructible<Fn, RValueCallable&>::value, "");
115790df145SEric Fiselier         static_assert(!std::is_constructible<Fn, RValueCallable>::value, "");
116790df145SEric Fiselier     }
117790df145SEric Fiselier #endif
1182df59c50SJF Bastien 
1192df59c50SJF Bastien   return 0;
1205a83710eSEric Fiselier }
121