xref: /freebsd/contrib/llvm-project/libcxx/include/functional (revision e40139ff33b48b56a24c808b166b04b8ee6f5b21)
10b57cec5SDimitry Andric// -*- C++ -*-
20b57cec5SDimitry Andric//===------------------------ functional ----------------------------------===//
30b57cec5SDimitry Andric//
40b57cec5SDimitry Andric// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
50b57cec5SDimitry Andric// See https://llvm.org/LICENSE.txt for license information.
60b57cec5SDimitry Andric// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
70b57cec5SDimitry Andric//
80b57cec5SDimitry Andric//===----------------------------------------------------------------------===//
90b57cec5SDimitry Andric
100b57cec5SDimitry Andric#ifndef _LIBCPP_FUNCTIONAL
110b57cec5SDimitry Andric#define _LIBCPP_FUNCTIONAL
120b57cec5SDimitry Andric
130b57cec5SDimitry Andric/*
140b57cec5SDimitry Andric    functional synopsis
150b57cec5SDimitry Andric
160b57cec5SDimitry Andricnamespace std
170b57cec5SDimitry Andric{
180b57cec5SDimitry Andric
190b57cec5SDimitry Andrictemplate <class Arg, class Result>
200b57cec5SDimitry Andricstruct unary_function
210b57cec5SDimitry Andric{
220b57cec5SDimitry Andric    typedef Arg    argument_type;
230b57cec5SDimitry Andric    typedef Result result_type;
240b57cec5SDimitry Andric};
250b57cec5SDimitry Andric
260b57cec5SDimitry Andrictemplate <class Arg1, class Arg2, class Result>
270b57cec5SDimitry Andricstruct binary_function
280b57cec5SDimitry Andric{
290b57cec5SDimitry Andric    typedef Arg1   first_argument_type;
300b57cec5SDimitry Andric    typedef Arg2   second_argument_type;
310b57cec5SDimitry Andric    typedef Result result_type;
320b57cec5SDimitry Andric};
330b57cec5SDimitry Andric
340b57cec5SDimitry Andrictemplate <class T>
350b57cec5SDimitry Andricclass reference_wrapper
360b57cec5SDimitry Andric    : public unary_function<T1, R> // if wrapping a unary functor
370b57cec5SDimitry Andric    : public binary_function<T1, T2, R> // if wraping a binary functor
380b57cec5SDimitry Andric{
390b57cec5SDimitry Andricpublic:
400b57cec5SDimitry Andric    // types
410b57cec5SDimitry Andric    typedef T type;
420b57cec5SDimitry Andric    typedef see below result_type; // Not always defined
430b57cec5SDimitry Andric
440b57cec5SDimitry Andric    // construct/copy/destroy
450b57cec5SDimitry Andric    reference_wrapper(T&) noexcept;
460b57cec5SDimitry Andric    reference_wrapper(T&&) = delete; // do not bind to temps
470b57cec5SDimitry Andric    reference_wrapper(const reference_wrapper<T>& x) noexcept;
480b57cec5SDimitry Andric
490b57cec5SDimitry Andric    // assignment
500b57cec5SDimitry Andric    reference_wrapper& operator=(const reference_wrapper<T>& x) noexcept;
510b57cec5SDimitry Andric
520b57cec5SDimitry Andric    // access
530b57cec5SDimitry Andric    operator T& () const noexcept;
540b57cec5SDimitry Andric    T& get() const noexcept;
550b57cec5SDimitry Andric
560b57cec5SDimitry Andric    // invoke
570b57cec5SDimitry Andric    template <class... ArgTypes>
580b57cec5SDimitry Andric      typename result_of<T&(ArgTypes&&...)>::type
590b57cec5SDimitry Andric          operator() (ArgTypes&&...) const;
600b57cec5SDimitry Andric};
610b57cec5SDimitry Andric
620b57cec5SDimitry Andrictemplate <class T> reference_wrapper<T> ref(T& t) noexcept;
630b57cec5SDimitry Andrictemplate <class T> void ref(const T&& t) = delete;
640b57cec5SDimitry Andrictemplate <class T> reference_wrapper<T> ref(reference_wrapper<T>t) noexcept;
650b57cec5SDimitry Andric
660b57cec5SDimitry Andrictemplate <class T> reference_wrapper<const T> cref(const T& t) noexcept;
670b57cec5SDimitry Andrictemplate <class T> void cref(const T&& t) = delete;
680b57cec5SDimitry Andrictemplate <class T> reference_wrapper<const T> cref(reference_wrapper<T> t) noexcept;
690b57cec5SDimitry Andric
700b57cec5SDimitry Andrictemplate <class T> struct unwrap_reference;                                       // since C++20
710b57cec5SDimitry Andrictemplate <class T> struct unwrap_ref_decay : unwrap_reference<decay_t<T>> { };    // since C++20
720b57cec5SDimitry Andrictemplate <class T> using unwrap_reference_t = typename unwrap_reference<T>::type; // since C++20
730b57cec5SDimitry Andrictemplate <class T> using unwrap_ref_decay_t = typename unwrap_ref_decay<T>::type; // since C++20
740b57cec5SDimitry Andric
750b57cec5SDimitry Andrictemplate <class T> // <class T=void> in C++14
760b57cec5SDimitry Andricstruct plus : binary_function<T, T, T>
770b57cec5SDimitry Andric{
780b57cec5SDimitry Andric    T operator()(const T& x, const T& y) const;
790b57cec5SDimitry Andric};
800b57cec5SDimitry Andric
810b57cec5SDimitry Andrictemplate <class T> // <class T=void> in C++14
820b57cec5SDimitry Andricstruct minus : binary_function<T, T, T>
830b57cec5SDimitry Andric{
840b57cec5SDimitry Andric    T operator()(const T& x, const T& y) const;
850b57cec5SDimitry Andric};
860b57cec5SDimitry Andric
870b57cec5SDimitry Andrictemplate <class T> // <class T=void> in C++14
880b57cec5SDimitry Andricstruct multiplies : binary_function<T, T, T>
890b57cec5SDimitry Andric{
900b57cec5SDimitry Andric    T operator()(const T& x, const T& y) const;
910b57cec5SDimitry Andric};
920b57cec5SDimitry Andric
930b57cec5SDimitry Andrictemplate <class T> // <class T=void> in C++14
940b57cec5SDimitry Andricstruct divides : binary_function<T, T, T>
950b57cec5SDimitry Andric{
960b57cec5SDimitry Andric    T operator()(const T& x, const T& y) const;
970b57cec5SDimitry Andric};
980b57cec5SDimitry Andric
990b57cec5SDimitry Andrictemplate <class T> // <class T=void> in C++14
1000b57cec5SDimitry Andricstruct modulus : binary_function<T, T, T>
1010b57cec5SDimitry Andric{
1020b57cec5SDimitry Andric    T operator()(const T& x, const T& y) const;
1030b57cec5SDimitry Andric};
1040b57cec5SDimitry Andric
1050b57cec5SDimitry Andrictemplate <class T> // <class T=void> in C++14
1060b57cec5SDimitry Andricstruct negate : unary_function<T, T>
1070b57cec5SDimitry Andric{
1080b57cec5SDimitry Andric    T operator()(const T& x) const;
1090b57cec5SDimitry Andric};
1100b57cec5SDimitry Andric
1110b57cec5SDimitry Andrictemplate <class T> // <class T=void> in C++14
1120b57cec5SDimitry Andricstruct equal_to : binary_function<T, T, bool>
1130b57cec5SDimitry Andric{
1140b57cec5SDimitry Andric    bool operator()(const T& x, const T& y) const;
1150b57cec5SDimitry Andric};
1160b57cec5SDimitry Andric
1170b57cec5SDimitry Andrictemplate <class T> // <class T=void> in C++14
1180b57cec5SDimitry Andricstruct not_equal_to : binary_function<T, T, bool>
1190b57cec5SDimitry Andric{
1200b57cec5SDimitry Andric    bool operator()(const T& x, const T& y) const;
1210b57cec5SDimitry Andric};
1220b57cec5SDimitry Andric
1230b57cec5SDimitry Andrictemplate <class T> // <class T=void> in C++14
1240b57cec5SDimitry Andricstruct greater : binary_function<T, T, bool>
1250b57cec5SDimitry Andric{
1260b57cec5SDimitry Andric    bool operator()(const T& x, const T& y) const;
1270b57cec5SDimitry Andric};
1280b57cec5SDimitry Andric
1290b57cec5SDimitry Andrictemplate <class T> // <class T=void> in C++14
1300b57cec5SDimitry Andricstruct less : binary_function<T, T, bool>
1310b57cec5SDimitry Andric{
1320b57cec5SDimitry Andric    bool operator()(const T& x, const T& y) const;
1330b57cec5SDimitry Andric};
1340b57cec5SDimitry Andric
1350b57cec5SDimitry Andrictemplate <class T> // <class T=void> in C++14
1360b57cec5SDimitry Andricstruct greater_equal : binary_function<T, T, bool>
1370b57cec5SDimitry Andric{
1380b57cec5SDimitry Andric    bool operator()(const T& x, const T& y) const;
1390b57cec5SDimitry Andric};
1400b57cec5SDimitry Andric
1410b57cec5SDimitry Andrictemplate <class T> // <class T=void> in C++14
1420b57cec5SDimitry Andricstruct less_equal : binary_function<T, T, bool>
1430b57cec5SDimitry Andric{
1440b57cec5SDimitry Andric    bool operator()(const T& x, const T& y) const;
1450b57cec5SDimitry Andric};
1460b57cec5SDimitry Andric
1470b57cec5SDimitry Andrictemplate <class T> // <class T=void> in C++14
1480b57cec5SDimitry Andricstruct logical_and : binary_function<T, T, bool>
1490b57cec5SDimitry Andric{
1500b57cec5SDimitry Andric    bool operator()(const T& x, const T& y) const;
1510b57cec5SDimitry Andric};
1520b57cec5SDimitry Andric
1530b57cec5SDimitry Andrictemplate <class T> // <class T=void> in C++14
1540b57cec5SDimitry Andricstruct logical_or : binary_function<T, T, bool>
1550b57cec5SDimitry Andric{
1560b57cec5SDimitry Andric    bool operator()(const T& x, const T& y) const;
1570b57cec5SDimitry Andric};
1580b57cec5SDimitry Andric
1590b57cec5SDimitry Andrictemplate <class T> // <class T=void> in C++14
1600b57cec5SDimitry Andricstruct logical_not : unary_function<T, bool>
1610b57cec5SDimitry Andric{
1620b57cec5SDimitry Andric    bool operator()(const T& x) const;
1630b57cec5SDimitry Andric};
1640b57cec5SDimitry Andric
1650b57cec5SDimitry Andrictemplate <class T> // <class T=void> in C++14
1660b57cec5SDimitry Andricstruct bit_and : unary_function<T, bool>
1670b57cec5SDimitry Andric{
1680b57cec5SDimitry Andric    bool operator()(const T& x, const T& y) const;
1690b57cec5SDimitry Andric};
1700b57cec5SDimitry Andric
1710b57cec5SDimitry Andrictemplate <class T> // <class T=void> in C++14
1720b57cec5SDimitry Andricstruct bit_or : unary_function<T, bool>
1730b57cec5SDimitry Andric{
1740b57cec5SDimitry Andric    bool operator()(const T& x, const T& y) const;
1750b57cec5SDimitry Andric};
1760b57cec5SDimitry Andric
1770b57cec5SDimitry Andrictemplate <class T> // <class T=void> in C++14
1780b57cec5SDimitry Andricstruct bit_xor : unary_function<T, bool>
1790b57cec5SDimitry Andric{
1800b57cec5SDimitry Andric    bool operator()(const T& x, const T& y) const;
1810b57cec5SDimitry Andric};
1820b57cec5SDimitry Andric
1830b57cec5SDimitry Andrictemplate <class T=void> // C++14
1840b57cec5SDimitry Andricstruct bit_xor : unary_function<T, bool>
1850b57cec5SDimitry Andric{
1860b57cec5SDimitry Andric    bool operator()(const T& x) const;
1870b57cec5SDimitry Andric};
1880b57cec5SDimitry Andric
1890b57cec5SDimitry Andrictemplate <class Predicate>
1900b57cec5SDimitry Andricclass unary_negate // deprecated in C++17
1910b57cec5SDimitry Andric    : public unary_function<typename Predicate::argument_type, bool>
1920b57cec5SDimitry Andric{
1930b57cec5SDimitry Andricpublic:
1940b57cec5SDimitry Andric    explicit unary_negate(const Predicate& pred);
1950b57cec5SDimitry Andric    bool operator()(const typename Predicate::argument_type& x) const;
1960b57cec5SDimitry Andric};
1970b57cec5SDimitry Andric
1980b57cec5SDimitry Andrictemplate <class Predicate> // deprecated in C++17
1990b57cec5SDimitry Andricunary_negate<Predicate> not1(const Predicate& pred);
2000b57cec5SDimitry Andric
2010b57cec5SDimitry Andrictemplate <class Predicate>
2020b57cec5SDimitry Andricclass binary_negate // deprecated in C++17
2030b57cec5SDimitry Andric    : public binary_function<typename Predicate::first_argument_type,
2040b57cec5SDimitry Andric                             typename Predicate::second_argument_type,
2050b57cec5SDimitry Andric                             bool>
2060b57cec5SDimitry Andric{
2070b57cec5SDimitry Andricpublic:
2080b57cec5SDimitry Andric    explicit binary_negate(const Predicate& pred);
2090b57cec5SDimitry Andric    bool operator()(const typename Predicate::first_argument_type& x,
2100b57cec5SDimitry Andric                    const typename Predicate::second_argument_type& y) const;
2110b57cec5SDimitry Andric};
2120b57cec5SDimitry Andric
2130b57cec5SDimitry Andrictemplate <class Predicate> // deprecated in C++17
2140b57cec5SDimitry Andricbinary_negate<Predicate> not2(const Predicate& pred);
2150b57cec5SDimitry Andric
2160b57cec5SDimitry Andrictemplate <class F> unspecified not_fn(F&& f); // C++17
2170b57cec5SDimitry Andric
2180b57cec5SDimitry Andrictemplate<class T> struct is_bind_expression;
2190b57cec5SDimitry Andrictemplate<class T> struct is_placeholder;
2200b57cec5SDimitry Andric
2210b57cec5SDimitry Andric    // See C++14 20.9.9, Function object binders
2220b57cec5SDimitry Andrictemplate <class T> inline constexpr bool is_bind_expression_v
2230b57cec5SDimitry Andric  = is_bind_expression<T>::value; // C++17
2240b57cec5SDimitry Andrictemplate <class T> inline constexpr int is_placeholder_v
2250b57cec5SDimitry Andric  = is_placeholder<T>::value; // C++17
2260b57cec5SDimitry Andric
2270b57cec5SDimitry Andric
2280b57cec5SDimitry Andrictemplate<class Fn, class... BoundArgs>
2290b57cec5SDimitry Andric  unspecified bind(Fn&&, BoundArgs&&...);
2300b57cec5SDimitry Andrictemplate<class R, class Fn, class... BoundArgs>
2310b57cec5SDimitry Andric  unspecified bind(Fn&&, BoundArgs&&...);
2320b57cec5SDimitry Andric
2330b57cec5SDimitry Andrictemplate<class F, class... Args>
2340b57cec5SDimitry Andric invoke_result_t<F, Args...> invoke(F&& f, Args&&... args) // C++17
2350b57cec5SDimitry Andric    noexcept(is_nothrow_invocable_v<F, Args...>);
2360b57cec5SDimitry Andric
2370b57cec5SDimitry Andricnamespace placeholders {
2380b57cec5SDimitry Andric  // M is the implementation-defined number of placeholders
2390b57cec5SDimitry Andric  extern unspecified _1;
2400b57cec5SDimitry Andric  extern unspecified _2;
2410b57cec5SDimitry Andric  .
2420b57cec5SDimitry Andric  .
2430b57cec5SDimitry Andric  .
2440b57cec5SDimitry Andric  extern unspecified _Mp;
2450b57cec5SDimitry Andric}
2460b57cec5SDimitry Andric
2470b57cec5SDimitry Andrictemplate <class Operation>
2480b57cec5SDimitry Andricclass binder1st     // deprecated in C++11, removed in C++17
2490b57cec5SDimitry Andric    : public unary_function<typename Operation::second_argument_type,
2500b57cec5SDimitry Andric                            typename Operation::result_type>
2510b57cec5SDimitry Andric{
2520b57cec5SDimitry Andricprotected:
2530b57cec5SDimitry Andric    Operation                               op;
2540b57cec5SDimitry Andric    typename Operation::first_argument_type value;
2550b57cec5SDimitry Andricpublic:
2560b57cec5SDimitry Andric    binder1st(const Operation& x, const typename Operation::first_argument_type y);
2570b57cec5SDimitry Andric    typename Operation::result_type operator()(      typename Operation::second_argument_type& x) const;
2580b57cec5SDimitry Andric    typename Operation::result_type operator()(const typename Operation::second_argument_type& x) const;
2590b57cec5SDimitry Andric};
2600b57cec5SDimitry Andric
2610b57cec5SDimitry Andrictemplate <class Operation, class T>
2620b57cec5SDimitry Andricbinder1st<Operation> bind1st(const Operation& op, const T& x);  // deprecated in C++11, removed in C++17
2630b57cec5SDimitry Andric
2640b57cec5SDimitry Andrictemplate <class Operation>
2650b57cec5SDimitry Andricclass binder2nd     // deprecated in C++11, removed in C++17
2660b57cec5SDimitry Andric    : public unary_function<typename Operation::first_argument_type,
2670b57cec5SDimitry Andric                            typename Operation::result_type>
2680b57cec5SDimitry Andric{
2690b57cec5SDimitry Andricprotected:
2700b57cec5SDimitry Andric    Operation                                op;
2710b57cec5SDimitry Andric    typename Operation::second_argument_type value;
2720b57cec5SDimitry Andricpublic:
2730b57cec5SDimitry Andric    binder2nd(const Operation& x, const typename Operation::second_argument_type y);
2740b57cec5SDimitry Andric    typename Operation::result_type operator()(      typename Operation::first_argument_type& x) const;
2750b57cec5SDimitry Andric    typename Operation::result_type operator()(const typename Operation::first_argument_type& x) const;
2760b57cec5SDimitry Andric};
2770b57cec5SDimitry Andric
2780b57cec5SDimitry Andrictemplate <class Operation, class T>
2790b57cec5SDimitry Andricbinder2nd<Operation> bind2nd(const Operation& op, const T& x);  // deprecated in C++11, removed in C++17
2800b57cec5SDimitry Andric
2810b57cec5SDimitry Andrictemplate <class Arg, class Result>      // deprecated in C++11, removed in C++17
2820b57cec5SDimitry Andricclass pointer_to_unary_function : public unary_function<Arg, Result>
2830b57cec5SDimitry Andric{
2840b57cec5SDimitry Andricpublic:
2850b57cec5SDimitry Andric    explicit pointer_to_unary_function(Result (*f)(Arg));
2860b57cec5SDimitry Andric    Result operator()(Arg x) const;
2870b57cec5SDimitry Andric};
2880b57cec5SDimitry Andric
2890b57cec5SDimitry Andrictemplate <class Arg, class Result>
2900b57cec5SDimitry Andricpointer_to_unary_function<Arg,Result> ptr_fun(Result (*f)(Arg));      // deprecated in C++11, removed in C++17
2910b57cec5SDimitry Andric
2920b57cec5SDimitry Andrictemplate <class Arg1, class Arg2, class Result>      // deprecated in C++11, removed in C++17
2930b57cec5SDimitry Andricclass pointer_to_binary_function : public binary_function<Arg1, Arg2, Result>
2940b57cec5SDimitry Andric{
2950b57cec5SDimitry Andricpublic:
2960b57cec5SDimitry Andric    explicit pointer_to_binary_function(Result (*f)(Arg1, Arg2));
2970b57cec5SDimitry Andric    Result operator()(Arg1 x, Arg2 y) const;
2980b57cec5SDimitry Andric};
2990b57cec5SDimitry Andric
3000b57cec5SDimitry Andrictemplate <class Arg1, class Arg2, class Result>
3010b57cec5SDimitry Andricpointer_to_binary_function<Arg1,Arg2,Result> ptr_fun(Result (*f)(Arg1,Arg2));      // deprecated in C++11, removed in C++17
3020b57cec5SDimitry Andric
3030b57cec5SDimitry Andrictemplate<class S, class T>      // deprecated in C++11, removed in C++17
3040b57cec5SDimitry Andricclass mem_fun_t : public unary_function<T*, S>
3050b57cec5SDimitry Andric{
3060b57cec5SDimitry Andricpublic:
3070b57cec5SDimitry Andric    explicit mem_fun_t(S (T::*p)());
3080b57cec5SDimitry Andric    S operator()(T* p) const;
3090b57cec5SDimitry Andric};
3100b57cec5SDimitry Andric
3110b57cec5SDimitry Andrictemplate<class S, class T, class A>
3120b57cec5SDimitry Andricclass mem_fun1_t : public binary_function<T*, A, S>      // deprecated in C++11, removed in C++17
3130b57cec5SDimitry Andric{
3140b57cec5SDimitry Andricpublic:
3150b57cec5SDimitry Andric    explicit mem_fun1_t(S (T::*p)(A));
3160b57cec5SDimitry Andric    S operator()(T* p, A x) const;
3170b57cec5SDimitry Andric};
3180b57cec5SDimitry Andric
3190b57cec5SDimitry Andrictemplate<class S, class T>          mem_fun_t<S,T>    mem_fun(S (T::*f)());      // deprecated in C++11, removed in C++17
3200b57cec5SDimitry Andrictemplate<class S, class T, class A> mem_fun1_t<S,T,A> mem_fun(S (T::*f)(A));     // deprecated in C++11, removed in C++17
3210b57cec5SDimitry Andric
3220b57cec5SDimitry Andrictemplate<class S, class T>
3230b57cec5SDimitry Andricclass mem_fun_ref_t : public unary_function<T, S>      // deprecated in C++11, removed in C++17
3240b57cec5SDimitry Andric{
3250b57cec5SDimitry Andricpublic:
3260b57cec5SDimitry Andric    explicit mem_fun_ref_t(S (T::*p)());
3270b57cec5SDimitry Andric    S operator()(T& p) const;
3280b57cec5SDimitry Andric};
3290b57cec5SDimitry Andric
3300b57cec5SDimitry Andrictemplate<class S, class T, class A>
3310b57cec5SDimitry Andricclass mem_fun1_ref_t : public binary_function<T, A, S>      // deprecated in C++11, removed in C++17
3320b57cec5SDimitry Andric{
3330b57cec5SDimitry Andricpublic:
3340b57cec5SDimitry Andric    explicit mem_fun1_ref_t(S (T::*p)(A));
3350b57cec5SDimitry Andric    S operator()(T& p, A x) const;
3360b57cec5SDimitry Andric};
3370b57cec5SDimitry Andric
3380b57cec5SDimitry Andrictemplate<class S, class T>          mem_fun_ref_t<S,T>    mem_fun_ref(S (T::*f)());      // deprecated in C++11, removed in C++17
3390b57cec5SDimitry Andrictemplate<class S, class T, class A> mem_fun1_ref_t<S,T,A> mem_fun_ref(S (T::*f)(A));     // deprecated in C++11, removed in C++17
3400b57cec5SDimitry Andric
3410b57cec5SDimitry Andrictemplate <class S, class T>
3420b57cec5SDimitry Andricclass const_mem_fun_t : public unary_function<const T*, S>      // deprecated in C++11, removed in C++17
3430b57cec5SDimitry Andric{
3440b57cec5SDimitry Andricpublic:
3450b57cec5SDimitry Andric    explicit const_mem_fun_t(S (T::*p)() const);
3460b57cec5SDimitry Andric    S operator()(const T* p) const;
3470b57cec5SDimitry Andric};
3480b57cec5SDimitry Andric
3490b57cec5SDimitry Andrictemplate <class S, class T, class A>
3500b57cec5SDimitry Andricclass const_mem_fun1_t : public binary_function<const T*, A, S>      // deprecated in C++11, removed in C++17
3510b57cec5SDimitry Andric{
3520b57cec5SDimitry Andricpublic:
3530b57cec5SDimitry Andric    explicit const_mem_fun1_t(S (T::*p)(A) const);
3540b57cec5SDimitry Andric    S operator()(const T* p, A x) const;
3550b57cec5SDimitry Andric};
3560b57cec5SDimitry Andric
3570b57cec5SDimitry Andrictemplate <class S, class T>          const_mem_fun_t<S,T>    mem_fun(S (T::*f)() const);      // deprecated in C++11, removed in C++17
3580b57cec5SDimitry Andrictemplate <class S, class T, class A> const_mem_fun1_t<S,T,A> mem_fun(S (T::*f)(A) const);     // deprecated in C++11, removed in C++17
3590b57cec5SDimitry Andric
3600b57cec5SDimitry Andrictemplate <class S, class T>
3610b57cec5SDimitry Andricclass const_mem_fun_ref_t : public unary_function<T, S>      // deprecated in C++11, removed in C++17
3620b57cec5SDimitry Andric{
3630b57cec5SDimitry Andricpublic:
3640b57cec5SDimitry Andric    explicit const_mem_fun_ref_t(S (T::*p)() const);
3650b57cec5SDimitry Andric    S operator()(const T& p) const;
3660b57cec5SDimitry Andric};
3670b57cec5SDimitry Andric
3680b57cec5SDimitry Andrictemplate <class S, class T, class A>
3690b57cec5SDimitry Andricclass const_mem_fun1_ref_t : public binary_function<T, A, S>      // deprecated in C++11, removed in C++17
3700b57cec5SDimitry Andric{
3710b57cec5SDimitry Andricpublic:
3720b57cec5SDimitry Andric    explicit const_mem_fun1_ref_t(S (T::*p)(A) const);
3730b57cec5SDimitry Andric    S operator()(const T& p, A x) const;
3740b57cec5SDimitry Andric};
3750b57cec5SDimitry Andric
3760b57cec5SDimitry Andrictemplate <class S, class T>          const_mem_fun_ref_t<S,T>    mem_fun_ref(S (T::*f)() const);   // deprecated in C++11, removed in C++17
3770b57cec5SDimitry Andrictemplate <class S, class T, class A> const_mem_fun1_ref_t<S,T,A> mem_fun_ref(S (T::*f)(A) const);  // deprecated in C++11, removed in C++17
3780b57cec5SDimitry Andric
3790b57cec5SDimitry Andrictemplate<class R, class T> unspecified mem_fn(R T::*);
3800b57cec5SDimitry Andric
3810b57cec5SDimitry Andricclass bad_function_call
3820b57cec5SDimitry Andric    : public exception
3830b57cec5SDimitry Andric{
3840b57cec5SDimitry Andric};
3850b57cec5SDimitry Andric
3860b57cec5SDimitry Andrictemplate<class> class function; // undefined
3870b57cec5SDimitry Andric
3880b57cec5SDimitry Andrictemplate<class R, class... ArgTypes>
3890b57cec5SDimitry Andricclass function<R(ArgTypes...)>
3900b57cec5SDimitry Andric  : public unary_function<T1, R>      // iff sizeof...(ArgTypes) == 1 and
3910b57cec5SDimitry Andric                                      // ArgTypes contains T1
3920b57cec5SDimitry Andric  : public binary_function<T1, T2, R> // iff sizeof...(ArgTypes) == 2 and
3930b57cec5SDimitry Andric                                      // ArgTypes contains T1 and T2
3940b57cec5SDimitry Andric{
3950b57cec5SDimitry Andricpublic:
3960b57cec5SDimitry Andric    typedef R result_type;
3970b57cec5SDimitry Andric
3980b57cec5SDimitry Andric    // construct/copy/destroy:
3990b57cec5SDimitry Andric    function() noexcept;
4000b57cec5SDimitry Andric    function(nullptr_t) noexcept;
4010b57cec5SDimitry Andric    function(const function&);
4020b57cec5SDimitry Andric    function(function&&) noexcept;
4030b57cec5SDimitry Andric    template<class F>
4040b57cec5SDimitry Andric      function(F);
4050b57cec5SDimitry Andric    template<Allocator Alloc>
4060b57cec5SDimitry Andric      function(allocator_arg_t, const Alloc&) noexcept;            // removed in C++17
4070b57cec5SDimitry Andric    template<Allocator Alloc>
4080b57cec5SDimitry Andric      function(allocator_arg_t, const Alloc&, nullptr_t) noexcept; // removed in C++17
4090b57cec5SDimitry Andric    template<Allocator Alloc>
4100b57cec5SDimitry Andric      function(allocator_arg_t, const Alloc&, const function&);    // removed in C++17
4110b57cec5SDimitry Andric    template<Allocator Alloc>
4120b57cec5SDimitry Andric      function(allocator_arg_t, const Alloc&, function&&);         // removed in C++17
4130b57cec5SDimitry Andric    template<class F, Allocator Alloc>
4140b57cec5SDimitry Andric      function(allocator_arg_t, const Alloc&, F);                  // removed in C++17
4150b57cec5SDimitry Andric
4160b57cec5SDimitry Andric    function& operator=(const function&);
4170b57cec5SDimitry Andric    function& operator=(function&&) noexcept;
4180b57cec5SDimitry Andric    function& operator=(nullptr_t) noexcept;
4190b57cec5SDimitry Andric    template<class F>
4200b57cec5SDimitry Andric      function& operator=(F&&);
4210b57cec5SDimitry Andric    template<class F>
4220b57cec5SDimitry Andric      function& operator=(reference_wrapper<F>) noexcept;
4230b57cec5SDimitry Andric
4240b57cec5SDimitry Andric    ~function();
4250b57cec5SDimitry Andric
4260b57cec5SDimitry Andric    // function modifiers:
4270b57cec5SDimitry Andric    void swap(function&) noexcept;
4280b57cec5SDimitry Andric    template<class F, class Alloc>
4290b57cec5SDimitry Andric      void assign(F&&, const Alloc&);                 // Removed in C++17
4300b57cec5SDimitry Andric
4310b57cec5SDimitry Andric    // function capacity:
4320b57cec5SDimitry Andric    explicit operator bool() const noexcept;
4330b57cec5SDimitry Andric
4340b57cec5SDimitry Andric    // function invocation:
4350b57cec5SDimitry Andric    R operator()(ArgTypes...) const;
4360b57cec5SDimitry Andric
4370b57cec5SDimitry Andric    // function target access:
4380b57cec5SDimitry Andric    const std::type_info& target_type() const noexcept;
4390b57cec5SDimitry Andric    template <typename T>       T* target() noexcept;
4400b57cec5SDimitry Andric    template <typename T> const T* target() const noexcept;
4410b57cec5SDimitry Andric};
4420b57cec5SDimitry Andric
443*e40139ffSDimitry Andric// Deduction guides
444*e40139ffSDimitry Andrictemplate<class R, class ...Args>
445*e40139ffSDimitry Andricfunction(R(*)(Args...)) -> function<R(Args...)>; // since C++17
446*e40139ffSDimitry Andric
447*e40139ffSDimitry Andrictemplate<class F>
448*e40139ffSDimitry Andricfunction(F) -> function<see-below>; // since C++17
449*e40139ffSDimitry Andric
4500b57cec5SDimitry Andric// Null pointer comparisons:
4510b57cec5SDimitry Andrictemplate <class R, class ... ArgTypes>
4520b57cec5SDimitry Andric  bool operator==(const function<R(ArgTypes...)>&, nullptr_t) noexcept;
4530b57cec5SDimitry Andric
4540b57cec5SDimitry Andrictemplate <class R, class ... ArgTypes>
4550b57cec5SDimitry Andric  bool operator==(nullptr_t, const function<R(ArgTypes...)>&) noexcept;
4560b57cec5SDimitry Andric
4570b57cec5SDimitry Andrictemplate <class R, class ... ArgTypes>
4580b57cec5SDimitry Andric  bool operator!=(const function<R(ArgTypes...)>&, nullptr_t) noexcept;
4590b57cec5SDimitry Andric
4600b57cec5SDimitry Andrictemplate <class  R, class ... ArgTypes>
4610b57cec5SDimitry Andric  bool operator!=(nullptr_t, const function<R(ArgTypes...)>&) noexcept;
4620b57cec5SDimitry Andric
4630b57cec5SDimitry Andric// specialized algorithms:
4640b57cec5SDimitry Andrictemplate <class  R, class ... ArgTypes>
4650b57cec5SDimitry Andric  void swap(function<R(ArgTypes...)>&, function<R(ArgTypes...)>&) noexcept;
4660b57cec5SDimitry Andric
4670b57cec5SDimitry Andrictemplate <class T> struct hash;
4680b57cec5SDimitry Andric
4690b57cec5SDimitry Andrictemplate <> struct hash<bool>;
4700b57cec5SDimitry Andrictemplate <> struct hash<char>;
4710b57cec5SDimitry Andrictemplate <> struct hash<signed char>;
4720b57cec5SDimitry Andrictemplate <> struct hash<unsigned char>;
4730b57cec5SDimitry Andrictemplate <> struct hash<char16_t>;
4740b57cec5SDimitry Andrictemplate <> struct hash<char32_t>;
4750b57cec5SDimitry Andrictemplate <> struct hash<wchar_t>;
4760b57cec5SDimitry Andrictemplate <> struct hash<short>;
4770b57cec5SDimitry Andrictemplate <> struct hash<unsigned short>;
4780b57cec5SDimitry Andrictemplate <> struct hash<int>;
4790b57cec5SDimitry Andrictemplate <> struct hash<unsigned int>;
4800b57cec5SDimitry Andrictemplate <> struct hash<long>;
4810b57cec5SDimitry Andrictemplate <> struct hash<long long>;
4820b57cec5SDimitry Andrictemplate <> struct hash<unsigned long>;
4830b57cec5SDimitry Andrictemplate <> struct hash<unsigned long long>;
4840b57cec5SDimitry Andric
4850b57cec5SDimitry Andrictemplate <> struct hash<float>;
4860b57cec5SDimitry Andrictemplate <> struct hash<double>;
4870b57cec5SDimitry Andrictemplate <> struct hash<long double>;
4880b57cec5SDimitry Andric
4890b57cec5SDimitry Andrictemplate<class T> struct hash<T*>;
4900b57cec5SDimitry Andrictemplate <> struct hash<nullptr_t>;  // C++17
4910b57cec5SDimitry Andric
4920b57cec5SDimitry Andric}  // std
4930b57cec5SDimitry Andric
4940b57cec5SDimitry AndricPOLICY:  For non-variadic implementations, the number of arguments is limited
4950b57cec5SDimitry Andric         to 3.  It is hoped that the need for non-variadic implementations
4960b57cec5SDimitry Andric         will be minimal.
4970b57cec5SDimitry Andric
4980b57cec5SDimitry Andric*/
4990b57cec5SDimitry Andric
5000b57cec5SDimitry Andric#include <__config>
5010b57cec5SDimitry Andric#include <type_traits>
5020b57cec5SDimitry Andric#include <typeinfo>
5030b57cec5SDimitry Andric#include <exception>
5040b57cec5SDimitry Andric#include <memory>
5050b57cec5SDimitry Andric#include <tuple>
5060b57cec5SDimitry Andric#include <utility>
5070b57cec5SDimitry Andric#include <version>
5080b57cec5SDimitry Andric
5090b57cec5SDimitry Andric#include <__functional_base>
5100b57cec5SDimitry Andric
5110b57cec5SDimitry Andric#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
5120b57cec5SDimitry Andric#pragma GCC system_header
5130b57cec5SDimitry Andric#endif
5140b57cec5SDimitry Andric
5150b57cec5SDimitry Andric_LIBCPP_BEGIN_NAMESPACE_STD
5160b57cec5SDimitry Andric
5170b57cec5SDimitry Andric#if _LIBCPP_STD_VER > 11
5180b57cec5SDimitry Andrictemplate <class _Tp = void>
5190b57cec5SDimitry Andric#else
5200b57cec5SDimitry Andrictemplate <class _Tp>
5210b57cec5SDimitry Andric#endif
5220b57cec5SDimitry Andricstruct _LIBCPP_TEMPLATE_VIS plus : binary_function<_Tp, _Tp, _Tp>
5230b57cec5SDimitry Andric{
5240b57cec5SDimitry Andric    _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
5250b57cec5SDimitry Andric    _Tp operator()(const _Tp& __x, const _Tp& __y) const
5260b57cec5SDimitry Andric        {return __x + __y;}
5270b57cec5SDimitry Andric};
5280b57cec5SDimitry Andric
5290b57cec5SDimitry Andric#if _LIBCPP_STD_VER > 11
5300b57cec5SDimitry Andrictemplate <>
5310b57cec5SDimitry Andricstruct _LIBCPP_TEMPLATE_VIS plus<void>
5320b57cec5SDimitry Andric{
5330b57cec5SDimitry Andric    template <class _T1, class _T2>
5340b57cec5SDimitry Andric    _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
5350b57cec5SDimitry Andric    auto operator()(_T1&& __t, _T2&& __u) const
5360b57cec5SDimitry Andric    _NOEXCEPT_(noexcept(_VSTD::forward<_T1>(__t) + _VSTD::forward<_T2>(__u)))
5370b57cec5SDimitry Andric    -> decltype        (_VSTD::forward<_T1>(__t) + _VSTD::forward<_T2>(__u))
5380b57cec5SDimitry Andric        { return        _VSTD::forward<_T1>(__t) + _VSTD::forward<_T2>(__u); }
5390b57cec5SDimitry Andric    typedef void is_transparent;
5400b57cec5SDimitry Andric};
5410b57cec5SDimitry Andric#endif
5420b57cec5SDimitry Andric
5430b57cec5SDimitry Andric
5440b57cec5SDimitry Andric#if _LIBCPP_STD_VER > 11
5450b57cec5SDimitry Andrictemplate <class _Tp = void>
5460b57cec5SDimitry Andric#else
5470b57cec5SDimitry Andrictemplate <class _Tp>
5480b57cec5SDimitry Andric#endif
5490b57cec5SDimitry Andricstruct _LIBCPP_TEMPLATE_VIS minus : binary_function<_Tp, _Tp, _Tp>
5500b57cec5SDimitry Andric{
5510b57cec5SDimitry Andric    _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
5520b57cec5SDimitry Andric    _Tp operator()(const _Tp& __x, const _Tp& __y) const
5530b57cec5SDimitry Andric        {return __x - __y;}
5540b57cec5SDimitry Andric};
5550b57cec5SDimitry Andric
5560b57cec5SDimitry Andric#if _LIBCPP_STD_VER > 11
5570b57cec5SDimitry Andrictemplate <>
5580b57cec5SDimitry Andricstruct _LIBCPP_TEMPLATE_VIS minus<void>
5590b57cec5SDimitry Andric{
5600b57cec5SDimitry Andric    template <class _T1, class _T2>
5610b57cec5SDimitry Andric    _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
5620b57cec5SDimitry Andric    auto operator()(_T1&& __t, _T2&& __u) const
5630b57cec5SDimitry Andric    _NOEXCEPT_(noexcept(_VSTD::forward<_T1>(__t) - _VSTD::forward<_T2>(__u)))
5640b57cec5SDimitry Andric    -> decltype        (_VSTD::forward<_T1>(__t) - _VSTD::forward<_T2>(__u))
5650b57cec5SDimitry Andric        { return        _VSTD::forward<_T1>(__t) - _VSTD::forward<_T2>(__u); }
5660b57cec5SDimitry Andric    typedef void is_transparent;
5670b57cec5SDimitry Andric};
5680b57cec5SDimitry Andric#endif
5690b57cec5SDimitry Andric
5700b57cec5SDimitry Andric
5710b57cec5SDimitry Andric#if _LIBCPP_STD_VER > 11
5720b57cec5SDimitry Andrictemplate <class _Tp = void>
5730b57cec5SDimitry Andric#else
5740b57cec5SDimitry Andrictemplate <class _Tp>
5750b57cec5SDimitry Andric#endif
5760b57cec5SDimitry Andricstruct _LIBCPP_TEMPLATE_VIS multiplies : binary_function<_Tp, _Tp, _Tp>
5770b57cec5SDimitry Andric{
5780b57cec5SDimitry Andric    _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
5790b57cec5SDimitry Andric    _Tp operator()(const _Tp& __x, const _Tp& __y) const
5800b57cec5SDimitry Andric        {return __x * __y;}
5810b57cec5SDimitry Andric};
5820b57cec5SDimitry Andric
5830b57cec5SDimitry Andric#if _LIBCPP_STD_VER > 11
5840b57cec5SDimitry Andrictemplate <>
5850b57cec5SDimitry Andricstruct _LIBCPP_TEMPLATE_VIS multiplies<void>
5860b57cec5SDimitry Andric{
5870b57cec5SDimitry Andric    template <class _T1, class _T2>
5880b57cec5SDimitry Andric    _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
5890b57cec5SDimitry Andric    auto operator()(_T1&& __t, _T2&& __u) const
5900b57cec5SDimitry Andric    _NOEXCEPT_(noexcept(_VSTD::forward<_T1>(__t) * _VSTD::forward<_T2>(__u)))
5910b57cec5SDimitry Andric    -> decltype        (_VSTD::forward<_T1>(__t) * _VSTD::forward<_T2>(__u))
5920b57cec5SDimitry Andric        { return        _VSTD::forward<_T1>(__t) * _VSTD::forward<_T2>(__u); }
5930b57cec5SDimitry Andric    typedef void is_transparent;
5940b57cec5SDimitry Andric};
5950b57cec5SDimitry Andric#endif
5960b57cec5SDimitry Andric
5970b57cec5SDimitry Andric
5980b57cec5SDimitry Andric#if _LIBCPP_STD_VER > 11
5990b57cec5SDimitry Andrictemplate <class _Tp = void>
6000b57cec5SDimitry Andric#else
6010b57cec5SDimitry Andrictemplate <class _Tp>
6020b57cec5SDimitry Andric#endif
6030b57cec5SDimitry Andricstruct _LIBCPP_TEMPLATE_VIS divides : binary_function<_Tp, _Tp, _Tp>
6040b57cec5SDimitry Andric{
6050b57cec5SDimitry Andric    _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
6060b57cec5SDimitry Andric    _Tp operator()(const _Tp& __x, const _Tp& __y) const
6070b57cec5SDimitry Andric        {return __x / __y;}
6080b57cec5SDimitry Andric};
6090b57cec5SDimitry Andric
6100b57cec5SDimitry Andric#if _LIBCPP_STD_VER > 11
6110b57cec5SDimitry Andrictemplate <>
6120b57cec5SDimitry Andricstruct _LIBCPP_TEMPLATE_VIS divides<void>
6130b57cec5SDimitry Andric{
6140b57cec5SDimitry Andric    template <class _T1, class _T2>
6150b57cec5SDimitry Andric    _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
6160b57cec5SDimitry Andric    auto operator()(_T1&& __t, _T2&& __u) const
6170b57cec5SDimitry Andric    _NOEXCEPT_(noexcept(_VSTD::forward<_T1>(__t) / _VSTD::forward<_T2>(__u)))
6180b57cec5SDimitry Andric    -> decltype        (_VSTD::forward<_T1>(__t) / _VSTD::forward<_T2>(__u))
6190b57cec5SDimitry Andric        { return        _VSTD::forward<_T1>(__t) / _VSTD::forward<_T2>(__u); }
6200b57cec5SDimitry Andric    typedef void is_transparent;
6210b57cec5SDimitry Andric};
6220b57cec5SDimitry Andric#endif
6230b57cec5SDimitry Andric
6240b57cec5SDimitry Andric
6250b57cec5SDimitry Andric#if _LIBCPP_STD_VER > 11
6260b57cec5SDimitry Andrictemplate <class _Tp = void>
6270b57cec5SDimitry Andric#else
6280b57cec5SDimitry Andrictemplate <class _Tp>
6290b57cec5SDimitry Andric#endif
6300b57cec5SDimitry Andricstruct _LIBCPP_TEMPLATE_VIS modulus : binary_function<_Tp, _Tp, _Tp>
6310b57cec5SDimitry Andric{
6320b57cec5SDimitry Andric    _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
6330b57cec5SDimitry Andric    _Tp operator()(const _Tp& __x, const _Tp& __y) const
6340b57cec5SDimitry Andric        {return __x % __y;}
6350b57cec5SDimitry Andric};
6360b57cec5SDimitry Andric
6370b57cec5SDimitry Andric#if _LIBCPP_STD_VER > 11
6380b57cec5SDimitry Andrictemplate <>
6390b57cec5SDimitry Andricstruct _LIBCPP_TEMPLATE_VIS modulus<void>
6400b57cec5SDimitry Andric{
6410b57cec5SDimitry Andric    template <class _T1, class _T2>
6420b57cec5SDimitry Andric    _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
6430b57cec5SDimitry Andric    auto operator()(_T1&& __t, _T2&& __u) const
6440b57cec5SDimitry Andric    _NOEXCEPT_(noexcept(_VSTD::forward<_T1>(__t) % _VSTD::forward<_T2>(__u)))
6450b57cec5SDimitry Andric    -> decltype        (_VSTD::forward<_T1>(__t) % _VSTD::forward<_T2>(__u))
6460b57cec5SDimitry Andric        { return        _VSTD::forward<_T1>(__t) % _VSTD::forward<_T2>(__u); }
6470b57cec5SDimitry Andric    typedef void is_transparent;
6480b57cec5SDimitry Andric};
6490b57cec5SDimitry Andric#endif
6500b57cec5SDimitry Andric
6510b57cec5SDimitry Andric
6520b57cec5SDimitry Andric#if _LIBCPP_STD_VER > 11
6530b57cec5SDimitry Andrictemplate <class _Tp = void>
6540b57cec5SDimitry Andric#else
6550b57cec5SDimitry Andrictemplate <class _Tp>
6560b57cec5SDimitry Andric#endif
6570b57cec5SDimitry Andricstruct _LIBCPP_TEMPLATE_VIS negate : unary_function<_Tp, _Tp>
6580b57cec5SDimitry Andric{
6590b57cec5SDimitry Andric    _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
6600b57cec5SDimitry Andric    _Tp operator()(const _Tp& __x) const
6610b57cec5SDimitry Andric        {return -__x;}
6620b57cec5SDimitry Andric};
6630b57cec5SDimitry Andric
6640b57cec5SDimitry Andric#if _LIBCPP_STD_VER > 11
6650b57cec5SDimitry Andrictemplate <>
6660b57cec5SDimitry Andricstruct _LIBCPP_TEMPLATE_VIS negate<void>
6670b57cec5SDimitry Andric{
6680b57cec5SDimitry Andric    template <class _Tp>
6690b57cec5SDimitry Andric    _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
6700b57cec5SDimitry Andric    auto operator()(_Tp&& __x) const
6710b57cec5SDimitry Andric    _NOEXCEPT_(noexcept(- _VSTD::forward<_Tp>(__x)))
6720b57cec5SDimitry Andric    -> decltype        (- _VSTD::forward<_Tp>(__x))
6730b57cec5SDimitry Andric        { return        - _VSTD::forward<_Tp>(__x); }
6740b57cec5SDimitry Andric    typedef void is_transparent;
6750b57cec5SDimitry Andric};
6760b57cec5SDimitry Andric#endif
6770b57cec5SDimitry Andric
6780b57cec5SDimitry Andric
6790b57cec5SDimitry Andric#if _LIBCPP_STD_VER > 11
6800b57cec5SDimitry Andrictemplate <class _Tp = void>
6810b57cec5SDimitry Andric#else
6820b57cec5SDimitry Andrictemplate <class _Tp>
6830b57cec5SDimitry Andric#endif
6840b57cec5SDimitry Andricstruct _LIBCPP_TEMPLATE_VIS equal_to : binary_function<_Tp, _Tp, bool>
6850b57cec5SDimitry Andric{
6860b57cec5SDimitry Andric    _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
6870b57cec5SDimitry Andric    bool operator()(const _Tp& __x, const _Tp& __y) const
6880b57cec5SDimitry Andric        {return __x == __y;}
6890b57cec5SDimitry Andric};
6900b57cec5SDimitry Andric
6910b57cec5SDimitry Andric#if _LIBCPP_STD_VER > 11
6920b57cec5SDimitry Andrictemplate <>
6930b57cec5SDimitry Andricstruct _LIBCPP_TEMPLATE_VIS equal_to<void>
6940b57cec5SDimitry Andric{
6950b57cec5SDimitry Andric    template <class _T1, class _T2>
6960b57cec5SDimitry Andric    _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
6970b57cec5SDimitry Andric    auto operator()(_T1&& __t, _T2&& __u) const
6980b57cec5SDimitry Andric    _NOEXCEPT_(noexcept(_VSTD::forward<_T1>(__t) == _VSTD::forward<_T2>(__u)))
6990b57cec5SDimitry Andric    -> decltype        (_VSTD::forward<_T1>(__t) == _VSTD::forward<_T2>(__u))
7000b57cec5SDimitry Andric        { return        _VSTD::forward<_T1>(__t) == _VSTD::forward<_T2>(__u); }
7010b57cec5SDimitry Andric    typedef void is_transparent;
7020b57cec5SDimitry Andric};
7030b57cec5SDimitry Andric#endif
7040b57cec5SDimitry Andric
7050b57cec5SDimitry Andric
7060b57cec5SDimitry Andric#if _LIBCPP_STD_VER > 11
7070b57cec5SDimitry Andrictemplate <class _Tp = void>
7080b57cec5SDimitry Andric#else
7090b57cec5SDimitry Andrictemplate <class _Tp>
7100b57cec5SDimitry Andric#endif
7110b57cec5SDimitry Andricstruct _LIBCPP_TEMPLATE_VIS not_equal_to : binary_function<_Tp, _Tp, bool>
7120b57cec5SDimitry Andric{
7130b57cec5SDimitry Andric    _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
7140b57cec5SDimitry Andric    bool operator()(const _Tp& __x, const _Tp& __y) const
7150b57cec5SDimitry Andric        {return __x != __y;}
7160b57cec5SDimitry Andric};
7170b57cec5SDimitry Andric
7180b57cec5SDimitry Andric#if _LIBCPP_STD_VER > 11
7190b57cec5SDimitry Andrictemplate <>
7200b57cec5SDimitry Andricstruct _LIBCPP_TEMPLATE_VIS not_equal_to<void>
7210b57cec5SDimitry Andric{
7220b57cec5SDimitry Andric    template <class _T1, class _T2>
7230b57cec5SDimitry Andric    _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
7240b57cec5SDimitry Andric    auto operator()(_T1&& __t, _T2&& __u) const
7250b57cec5SDimitry Andric    _NOEXCEPT_(noexcept(_VSTD::forward<_T1>(__t) != _VSTD::forward<_T2>(__u)))
7260b57cec5SDimitry Andric    -> decltype        (_VSTD::forward<_T1>(__t) != _VSTD::forward<_T2>(__u))
7270b57cec5SDimitry Andric        { return        _VSTD::forward<_T1>(__t) != _VSTD::forward<_T2>(__u); }
7280b57cec5SDimitry Andric    typedef void is_transparent;
7290b57cec5SDimitry Andric};
7300b57cec5SDimitry Andric#endif
7310b57cec5SDimitry Andric
7320b57cec5SDimitry Andric
7330b57cec5SDimitry Andric#if _LIBCPP_STD_VER > 11
7340b57cec5SDimitry Andrictemplate <class _Tp = void>
7350b57cec5SDimitry Andric#else
7360b57cec5SDimitry Andrictemplate <class _Tp>
7370b57cec5SDimitry Andric#endif
7380b57cec5SDimitry Andricstruct _LIBCPP_TEMPLATE_VIS greater : binary_function<_Tp, _Tp, bool>
7390b57cec5SDimitry Andric{
7400b57cec5SDimitry Andric    _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
7410b57cec5SDimitry Andric    bool operator()(const _Tp& __x, const _Tp& __y) const
7420b57cec5SDimitry Andric        {return __x > __y;}
7430b57cec5SDimitry Andric};
7440b57cec5SDimitry Andric
7450b57cec5SDimitry Andric#if _LIBCPP_STD_VER > 11
7460b57cec5SDimitry Andrictemplate <>
7470b57cec5SDimitry Andricstruct _LIBCPP_TEMPLATE_VIS greater<void>
7480b57cec5SDimitry Andric{
7490b57cec5SDimitry Andric    template <class _T1, class _T2>
7500b57cec5SDimitry Andric    _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
7510b57cec5SDimitry Andric    auto operator()(_T1&& __t, _T2&& __u) const
7520b57cec5SDimitry Andric    _NOEXCEPT_(noexcept(_VSTD::forward<_T1>(__t) > _VSTD::forward<_T2>(__u)))
7530b57cec5SDimitry Andric    -> decltype        (_VSTD::forward<_T1>(__t) > _VSTD::forward<_T2>(__u))
7540b57cec5SDimitry Andric        { return        _VSTD::forward<_T1>(__t) > _VSTD::forward<_T2>(__u); }
7550b57cec5SDimitry Andric    typedef void is_transparent;
7560b57cec5SDimitry Andric};
7570b57cec5SDimitry Andric#endif
7580b57cec5SDimitry Andric
7590b57cec5SDimitry Andric
7600b57cec5SDimitry Andric// less in <__functional_base>
7610b57cec5SDimitry Andric
7620b57cec5SDimitry Andric#if _LIBCPP_STD_VER > 11
7630b57cec5SDimitry Andrictemplate <class _Tp = void>
7640b57cec5SDimitry Andric#else
7650b57cec5SDimitry Andrictemplate <class _Tp>
7660b57cec5SDimitry Andric#endif
7670b57cec5SDimitry Andricstruct _LIBCPP_TEMPLATE_VIS greater_equal : binary_function<_Tp, _Tp, bool>
7680b57cec5SDimitry Andric{
7690b57cec5SDimitry Andric    _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
7700b57cec5SDimitry Andric    bool operator()(const _Tp& __x, const _Tp& __y) const
7710b57cec5SDimitry Andric        {return __x >= __y;}
7720b57cec5SDimitry Andric};
7730b57cec5SDimitry Andric
7740b57cec5SDimitry Andric#if _LIBCPP_STD_VER > 11
7750b57cec5SDimitry Andrictemplate <>
7760b57cec5SDimitry Andricstruct _LIBCPP_TEMPLATE_VIS greater_equal<void>
7770b57cec5SDimitry Andric{
7780b57cec5SDimitry Andric    template <class _T1, class _T2>
7790b57cec5SDimitry Andric    _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
7800b57cec5SDimitry Andric    auto operator()(_T1&& __t, _T2&& __u) const
7810b57cec5SDimitry Andric    _NOEXCEPT_(noexcept(_VSTD::forward<_T1>(__t) >= _VSTD::forward<_T2>(__u)))
7820b57cec5SDimitry Andric    -> decltype        (_VSTD::forward<_T1>(__t) >= _VSTD::forward<_T2>(__u))
7830b57cec5SDimitry Andric        { return        _VSTD::forward<_T1>(__t) >= _VSTD::forward<_T2>(__u); }
7840b57cec5SDimitry Andric    typedef void is_transparent;
7850b57cec5SDimitry Andric};
7860b57cec5SDimitry Andric#endif
7870b57cec5SDimitry Andric
7880b57cec5SDimitry Andric
7890b57cec5SDimitry Andric#if _LIBCPP_STD_VER > 11
7900b57cec5SDimitry Andrictemplate <class _Tp = void>
7910b57cec5SDimitry Andric#else
7920b57cec5SDimitry Andrictemplate <class _Tp>
7930b57cec5SDimitry Andric#endif
7940b57cec5SDimitry Andricstruct _LIBCPP_TEMPLATE_VIS less_equal : binary_function<_Tp, _Tp, bool>
7950b57cec5SDimitry Andric{
7960b57cec5SDimitry Andric    _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
7970b57cec5SDimitry Andric    bool operator()(const _Tp& __x, const _Tp& __y) const
7980b57cec5SDimitry Andric        {return __x <= __y;}
7990b57cec5SDimitry Andric};
8000b57cec5SDimitry Andric
8010b57cec5SDimitry Andric#if _LIBCPP_STD_VER > 11
8020b57cec5SDimitry Andrictemplate <>
8030b57cec5SDimitry Andricstruct _LIBCPP_TEMPLATE_VIS less_equal<void>
8040b57cec5SDimitry Andric{
8050b57cec5SDimitry Andric    template <class _T1, class _T2>
8060b57cec5SDimitry Andric    _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
8070b57cec5SDimitry Andric    auto operator()(_T1&& __t, _T2&& __u) const
8080b57cec5SDimitry Andric    _NOEXCEPT_(noexcept(_VSTD::forward<_T1>(__t) <= _VSTD::forward<_T2>(__u)))
8090b57cec5SDimitry Andric    -> decltype        (_VSTD::forward<_T1>(__t) <= _VSTD::forward<_T2>(__u))
8100b57cec5SDimitry Andric        { return        _VSTD::forward<_T1>(__t) <= _VSTD::forward<_T2>(__u); }
8110b57cec5SDimitry Andric    typedef void is_transparent;
8120b57cec5SDimitry Andric};
8130b57cec5SDimitry Andric#endif
8140b57cec5SDimitry Andric
8150b57cec5SDimitry Andric
8160b57cec5SDimitry Andric#if _LIBCPP_STD_VER > 11
8170b57cec5SDimitry Andrictemplate <class _Tp = void>
8180b57cec5SDimitry Andric#else
8190b57cec5SDimitry Andrictemplate <class _Tp>
8200b57cec5SDimitry Andric#endif
8210b57cec5SDimitry Andricstruct _LIBCPP_TEMPLATE_VIS logical_and : binary_function<_Tp, _Tp, bool>
8220b57cec5SDimitry Andric{
8230b57cec5SDimitry Andric    _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
8240b57cec5SDimitry Andric    bool operator()(const _Tp& __x, const _Tp& __y) const
8250b57cec5SDimitry Andric        {return __x && __y;}
8260b57cec5SDimitry Andric};
8270b57cec5SDimitry Andric
8280b57cec5SDimitry Andric#if _LIBCPP_STD_VER > 11
8290b57cec5SDimitry Andrictemplate <>
8300b57cec5SDimitry Andricstruct _LIBCPP_TEMPLATE_VIS logical_and<void>
8310b57cec5SDimitry Andric{
8320b57cec5SDimitry Andric    template <class _T1, class _T2>
8330b57cec5SDimitry Andric    _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
8340b57cec5SDimitry Andric    auto operator()(_T1&& __t, _T2&& __u) const
8350b57cec5SDimitry Andric    _NOEXCEPT_(noexcept(_VSTD::forward<_T1>(__t) && _VSTD::forward<_T2>(__u)))
8360b57cec5SDimitry Andric    -> decltype        (_VSTD::forward<_T1>(__t) && _VSTD::forward<_T2>(__u))
8370b57cec5SDimitry Andric        { return        _VSTD::forward<_T1>(__t) && _VSTD::forward<_T2>(__u); }
8380b57cec5SDimitry Andric    typedef void is_transparent;
8390b57cec5SDimitry Andric};
8400b57cec5SDimitry Andric#endif
8410b57cec5SDimitry Andric
8420b57cec5SDimitry Andric
8430b57cec5SDimitry Andric#if _LIBCPP_STD_VER > 11
8440b57cec5SDimitry Andrictemplate <class _Tp = void>
8450b57cec5SDimitry Andric#else
8460b57cec5SDimitry Andrictemplate <class _Tp>
8470b57cec5SDimitry Andric#endif
8480b57cec5SDimitry Andricstruct _LIBCPP_TEMPLATE_VIS logical_or : binary_function<_Tp, _Tp, bool>
8490b57cec5SDimitry Andric{
8500b57cec5SDimitry Andric    _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
8510b57cec5SDimitry Andric    bool operator()(const _Tp& __x, const _Tp& __y) const
8520b57cec5SDimitry Andric        {return __x || __y;}
8530b57cec5SDimitry Andric};
8540b57cec5SDimitry Andric
8550b57cec5SDimitry Andric#if _LIBCPP_STD_VER > 11
8560b57cec5SDimitry Andrictemplate <>
8570b57cec5SDimitry Andricstruct _LIBCPP_TEMPLATE_VIS logical_or<void>
8580b57cec5SDimitry Andric{
8590b57cec5SDimitry Andric    template <class _T1, class _T2>
8600b57cec5SDimitry Andric    _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
8610b57cec5SDimitry Andric    auto operator()(_T1&& __t, _T2&& __u) const
8620b57cec5SDimitry Andric    _NOEXCEPT_(noexcept(_VSTD::forward<_T1>(__t) || _VSTD::forward<_T2>(__u)))
8630b57cec5SDimitry Andric    -> decltype        (_VSTD::forward<_T1>(__t) || _VSTD::forward<_T2>(__u))
8640b57cec5SDimitry Andric        { return        _VSTD::forward<_T1>(__t) || _VSTD::forward<_T2>(__u); }
8650b57cec5SDimitry Andric    typedef void is_transparent;
8660b57cec5SDimitry Andric};
8670b57cec5SDimitry Andric#endif
8680b57cec5SDimitry Andric
8690b57cec5SDimitry Andric
8700b57cec5SDimitry Andric#if _LIBCPP_STD_VER > 11
8710b57cec5SDimitry Andrictemplate <class _Tp = void>
8720b57cec5SDimitry Andric#else
8730b57cec5SDimitry Andrictemplate <class _Tp>
8740b57cec5SDimitry Andric#endif
8750b57cec5SDimitry Andricstruct _LIBCPP_TEMPLATE_VIS logical_not : unary_function<_Tp, bool>
8760b57cec5SDimitry Andric{
8770b57cec5SDimitry Andric    _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
8780b57cec5SDimitry Andric    bool operator()(const _Tp& __x) const
8790b57cec5SDimitry Andric        {return !__x;}
8800b57cec5SDimitry Andric};
8810b57cec5SDimitry Andric
8820b57cec5SDimitry Andric#if _LIBCPP_STD_VER > 11
8830b57cec5SDimitry Andrictemplate <>
8840b57cec5SDimitry Andricstruct _LIBCPP_TEMPLATE_VIS logical_not<void>
8850b57cec5SDimitry Andric{
8860b57cec5SDimitry Andric    template <class _Tp>
8870b57cec5SDimitry Andric    _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
8880b57cec5SDimitry Andric    auto operator()(_Tp&& __x) const
8890b57cec5SDimitry Andric    _NOEXCEPT_(noexcept(!_VSTD::forward<_Tp>(__x)))
8900b57cec5SDimitry Andric    -> decltype        (!_VSTD::forward<_Tp>(__x))
8910b57cec5SDimitry Andric        { return        !_VSTD::forward<_Tp>(__x); }
8920b57cec5SDimitry Andric    typedef void is_transparent;
8930b57cec5SDimitry Andric};
8940b57cec5SDimitry Andric#endif
8950b57cec5SDimitry Andric
8960b57cec5SDimitry Andric
8970b57cec5SDimitry Andric#if _LIBCPP_STD_VER > 11
8980b57cec5SDimitry Andrictemplate <class _Tp = void>
8990b57cec5SDimitry Andric#else
9000b57cec5SDimitry Andrictemplate <class _Tp>
9010b57cec5SDimitry Andric#endif
9020b57cec5SDimitry Andricstruct _LIBCPP_TEMPLATE_VIS bit_and : binary_function<_Tp, _Tp, _Tp>
9030b57cec5SDimitry Andric{
9040b57cec5SDimitry Andric    _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
9050b57cec5SDimitry Andric    _Tp operator()(const _Tp& __x, const _Tp& __y) const
9060b57cec5SDimitry Andric        {return __x & __y;}
9070b57cec5SDimitry Andric};
9080b57cec5SDimitry Andric
9090b57cec5SDimitry Andric#if _LIBCPP_STD_VER > 11
9100b57cec5SDimitry Andrictemplate <>
9110b57cec5SDimitry Andricstruct _LIBCPP_TEMPLATE_VIS bit_and<void>
9120b57cec5SDimitry Andric{
9130b57cec5SDimitry Andric    template <class _T1, class _T2>
9140b57cec5SDimitry Andric    _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
9150b57cec5SDimitry Andric    auto operator()(_T1&& __t, _T2&& __u) const
9160b57cec5SDimitry Andric    _NOEXCEPT_(noexcept(_VSTD::forward<_T1>(__t) & _VSTD::forward<_T2>(__u)))
9170b57cec5SDimitry Andric    -> decltype        (_VSTD::forward<_T1>(__t) & _VSTD::forward<_T2>(__u))
9180b57cec5SDimitry Andric        { return        _VSTD::forward<_T1>(__t) & _VSTD::forward<_T2>(__u); }
9190b57cec5SDimitry Andric    typedef void is_transparent;
9200b57cec5SDimitry Andric};
9210b57cec5SDimitry Andric#endif
9220b57cec5SDimitry Andric
9230b57cec5SDimitry Andric
9240b57cec5SDimitry Andric#if _LIBCPP_STD_VER > 11
9250b57cec5SDimitry Andrictemplate <class _Tp = void>
9260b57cec5SDimitry Andric#else
9270b57cec5SDimitry Andrictemplate <class _Tp>
9280b57cec5SDimitry Andric#endif
9290b57cec5SDimitry Andricstruct _LIBCPP_TEMPLATE_VIS bit_or : binary_function<_Tp, _Tp, _Tp>
9300b57cec5SDimitry Andric{
9310b57cec5SDimitry Andric    _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
9320b57cec5SDimitry Andric    _Tp operator()(const _Tp& __x, const _Tp& __y) const
9330b57cec5SDimitry Andric        {return __x | __y;}
9340b57cec5SDimitry Andric};
9350b57cec5SDimitry Andric
9360b57cec5SDimitry Andric#if _LIBCPP_STD_VER > 11
9370b57cec5SDimitry Andrictemplate <>
9380b57cec5SDimitry Andricstruct _LIBCPP_TEMPLATE_VIS bit_or<void>
9390b57cec5SDimitry Andric{
9400b57cec5SDimitry Andric    template <class _T1, class _T2>
9410b57cec5SDimitry Andric    _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
9420b57cec5SDimitry Andric    auto operator()(_T1&& __t, _T2&& __u) const
9430b57cec5SDimitry Andric    _NOEXCEPT_(noexcept(_VSTD::forward<_T1>(__t) | _VSTD::forward<_T2>(__u)))
9440b57cec5SDimitry Andric    -> decltype        (_VSTD::forward<_T1>(__t) | _VSTD::forward<_T2>(__u))
9450b57cec5SDimitry Andric        { return        _VSTD::forward<_T1>(__t) | _VSTD::forward<_T2>(__u); }
9460b57cec5SDimitry Andric    typedef void is_transparent;
9470b57cec5SDimitry Andric};
9480b57cec5SDimitry Andric#endif
9490b57cec5SDimitry Andric
9500b57cec5SDimitry Andric
9510b57cec5SDimitry Andric#if _LIBCPP_STD_VER > 11
9520b57cec5SDimitry Andrictemplate <class _Tp = void>
9530b57cec5SDimitry Andric#else
9540b57cec5SDimitry Andrictemplate <class _Tp>
9550b57cec5SDimitry Andric#endif
9560b57cec5SDimitry Andricstruct _LIBCPP_TEMPLATE_VIS bit_xor : binary_function<_Tp, _Tp, _Tp>
9570b57cec5SDimitry Andric{
9580b57cec5SDimitry Andric    _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
9590b57cec5SDimitry Andric    _Tp operator()(const _Tp& __x, const _Tp& __y) const
9600b57cec5SDimitry Andric        {return __x ^ __y;}
9610b57cec5SDimitry Andric};
9620b57cec5SDimitry Andric
9630b57cec5SDimitry Andric#if _LIBCPP_STD_VER > 11
9640b57cec5SDimitry Andrictemplate <>
9650b57cec5SDimitry Andricstruct _LIBCPP_TEMPLATE_VIS bit_xor<void>
9660b57cec5SDimitry Andric{
9670b57cec5SDimitry Andric    template <class _T1, class _T2>
9680b57cec5SDimitry Andric    _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
9690b57cec5SDimitry Andric    auto operator()(_T1&& __t, _T2&& __u) const
9700b57cec5SDimitry Andric    _NOEXCEPT_(noexcept(_VSTD::forward<_T1>(__t) ^ _VSTD::forward<_T2>(__u)))
9710b57cec5SDimitry Andric    -> decltype        (_VSTD::forward<_T1>(__t) ^ _VSTD::forward<_T2>(__u))
9720b57cec5SDimitry Andric        { return        _VSTD::forward<_T1>(__t) ^ _VSTD::forward<_T2>(__u); }
9730b57cec5SDimitry Andric    typedef void is_transparent;
9740b57cec5SDimitry Andric};
9750b57cec5SDimitry Andric#endif
9760b57cec5SDimitry Andric
9770b57cec5SDimitry Andric
9780b57cec5SDimitry Andric#if _LIBCPP_STD_VER > 11
9790b57cec5SDimitry Andrictemplate <class _Tp = void>
9800b57cec5SDimitry Andricstruct _LIBCPP_TEMPLATE_VIS bit_not : unary_function<_Tp, _Tp>
9810b57cec5SDimitry Andric{
9820b57cec5SDimitry Andric    _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
9830b57cec5SDimitry Andric    _Tp operator()(const _Tp& __x) const
9840b57cec5SDimitry Andric        {return ~__x;}
9850b57cec5SDimitry Andric};
9860b57cec5SDimitry Andric
9870b57cec5SDimitry Andrictemplate <>
9880b57cec5SDimitry Andricstruct _LIBCPP_TEMPLATE_VIS bit_not<void>
9890b57cec5SDimitry Andric{
9900b57cec5SDimitry Andric    template <class _Tp>
9910b57cec5SDimitry Andric    _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
9920b57cec5SDimitry Andric    auto operator()(_Tp&& __x) const
9930b57cec5SDimitry Andric    _NOEXCEPT_(noexcept(~_VSTD::forward<_Tp>(__x)))
9940b57cec5SDimitry Andric    -> decltype        (~_VSTD::forward<_Tp>(__x))
9950b57cec5SDimitry Andric        { return        ~_VSTD::forward<_Tp>(__x); }
9960b57cec5SDimitry Andric    typedef void is_transparent;
9970b57cec5SDimitry Andric};
9980b57cec5SDimitry Andric#endif
9990b57cec5SDimitry Andric
10000b57cec5SDimitry Andrictemplate <class _Predicate>
10010b57cec5SDimitry Andricclass _LIBCPP_TEMPLATE_VIS _LIBCPP_DEPRECATED_IN_CXX17 unary_negate
10020b57cec5SDimitry Andric    : public unary_function<typename _Predicate::argument_type, bool>
10030b57cec5SDimitry Andric{
10040b57cec5SDimitry Andric    _Predicate __pred_;
10050b57cec5SDimitry Andricpublic:
10060b57cec5SDimitry Andric    _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
10070b57cec5SDimitry Andric    explicit unary_negate(const _Predicate& __pred)
10080b57cec5SDimitry Andric        : __pred_(__pred) {}
10090b57cec5SDimitry Andric    _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
10100b57cec5SDimitry Andric    bool operator()(const typename _Predicate::argument_type& __x) const
10110b57cec5SDimitry Andric        {return !__pred_(__x);}
10120b57cec5SDimitry Andric};
10130b57cec5SDimitry Andric
10140b57cec5SDimitry Andrictemplate <class _Predicate>
10150b57cec5SDimitry Andric_LIBCPP_DEPRECATED_IN_CXX17 inline _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
10160b57cec5SDimitry Andricunary_negate<_Predicate>
10170b57cec5SDimitry Andricnot1(const _Predicate& __pred) {return unary_negate<_Predicate>(__pred);}
10180b57cec5SDimitry Andric
10190b57cec5SDimitry Andrictemplate <class _Predicate>
10200b57cec5SDimitry Andricclass _LIBCPP_TEMPLATE_VIS _LIBCPP_DEPRECATED_IN_CXX17 binary_negate
10210b57cec5SDimitry Andric    : public binary_function<typename _Predicate::first_argument_type,
10220b57cec5SDimitry Andric                             typename _Predicate::second_argument_type,
10230b57cec5SDimitry Andric                             bool>
10240b57cec5SDimitry Andric{
10250b57cec5SDimitry Andric    _Predicate __pred_;
10260b57cec5SDimitry Andricpublic:
10270b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY explicit _LIBCPP_CONSTEXPR_AFTER_CXX11
10280b57cec5SDimitry Andric    binary_negate(const _Predicate& __pred) : __pred_(__pred) {}
10290b57cec5SDimitry Andric
10300b57cec5SDimitry Andric    _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
10310b57cec5SDimitry Andric    bool operator()(const typename _Predicate::first_argument_type& __x,
10320b57cec5SDimitry Andric                    const typename _Predicate::second_argument_type& __y) const
10330b57cec5SDimitry Andric        {return !__pred_(__x, __y);}
10340b57cec5SDimitry Andric};
10350b57cec5SDimitry Andric
10360b57cec5SDimitry Andrictemplate <class _Predicate>
10370b57cec5SDimitry Andric_LIBCPP_DEPRECATED_IN_CXX17 inline _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
10380b57cec5SDimitry Andricbinary_negate<_Predicate>
10390b57cec5SDimitry Andricnot2(const _Predicate& __pred) {return binary_negate<_Predicate>(__pred);}
10400b57cec5SDimitry Andric
10410b57cec5SDimitry Andric#if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_BINDERS)
10420b57cec5SDimitry Andrictemplate <class __Operation>
10430b57cec5SDimitry Andricclass _LIBCPP_TEMPLATE_VIS _LIBCPP_DEPRECATED_IN_CXX11 binder1st
10440b57cec5SDimitry Andric    : public unary_function<typename __Operation::second_argument_type,
10450b57cec5SDimitry Andric                            typename __Operation::result_type>
10460b57cec5SDimitry Andric{
10470b57cec5SDimitry Andricprotected:
10480b57cec5SDimitry Andric    __Operation                               op;
10490b57cec5SDimitry Andric    typename __Operation::first_argument_type value;
10500b57cec5SDimitry Andricpublic:
10510b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY binder1st(const __Operation& __x,
10520b57cec5SDimitry Andric                               const typename __Operation::first_argument_type __y)
10530b57cec5SDimitry Andric        : op(__x), value(__y) {}
10540b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY typename __Operation::result_type operator()
10550b57cec5SDimitry Andric        (typename __Operation::second_argument_type& __x) const
10560b57cec5SDimitry Andric            {return op(value, __x);}
10570b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY typename __Operation::result_type operator()
10580b57cec5SDimitry Andric        (const typename __Operation::second_argument_type& __x) const
10590b57cec5SDimitry Andric            {return op(value, __x);}
10600b57cec5SDimitry Andric};
10610b57cec5SDimitry Andric
10620b57cec5SDimitry Andrictemplate <class __Operation, class _Tp>
10630b57cec5SDimitry Andric_LIBCPP_DEPRECATED_IN_CXX11 inline _LIBCPP_INLINE_VISIBILITY
10640b57cec5SDimitry Andricbinder1st<__Operation>
10650b57cec5SDimitry Andricbind1st(const __Operation& __op, const _Tp& __x)
10660b57cec5SDimitry Andric    {return binder1st<__Operation>(__op, __x);}
10670b57cec5SDimitry Andric
10680b57cec5SDimitry Andrictemplate <class __Operation>
10690b57cec5SDimitry Andricclass _LIBCPP_TEMPLATE_VIS _LIBCPP_DEPRECATED_IN_CXX11 binder2nd
10700b57cec5SDimitry Andric    : public unary_function<typename __Operation::first_argument_type,
10710b57cec5SDimitry Andric                            typename __Operation::result_type>
10720b57cec5SDimitry Andric{
10730b57cec5SDimitry Andricprotected:
10740b57cec5SDimitry Andric    __Operation                                op;
10750b57cec5SDimitry Andric    typename __Operation::second_argument_type value;
10760b57cec5SDimitry Andricpublic:
10770b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
10780b57cec5SDimitry Andric    binder2nd(const __Operation& __x, const typename __Operation::second_argument_type __y)
10790b57cec5SDimitry Andric        : op(__x), value(__y) {}
10800b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY typename __Operation::result_type operator()
10810b57cec5SDimitry Andric        (      typename __Operation::first_argument_type& __x) const
10820b57cec5SDimitry Andric            {return op(__x, value);}
10830b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY typename __Operation::result_type operator()
10840b57cec5SDimitry Andric        (const typename __Operation::first_argument_type& __x) const
10850b57cec5SDimitry Andric            {return op(__x, value);}
10860b57cec5SDimitry Andric};
10870b57cec5SDimitry Andric
10880b57cec5SDimitry Andrictemplate <class __Operation, class _Tp>
10890b57cec5SDimitry Andric_LIBCPP_DEPRECATED_IN_CXX11 inline _LIBCPP_INLINE_VISIBILITY
10900b57cec5SDimitry Andricbinder2nd<__Operation>
10910b57cec5SDimitry Andricbind2nd(const __Operation& __op, const _Tp& __x)
10920b57cec5SDimitry Andric    {return binder2nd<__Operation>(__op, __x);}
10930b57cec5SDimitry Andric
10940b57cec5SDimitry Andrictemplate <class _Arg, class _Result>
10950b57cec5SDimitry Andricclass _LIBCPP_TEMPLATE_VIS _LIBCPP_DEPRECATED_IN_CXX11 pointer_to_unary_function
10960b57cec5SDimitry Andric    : public unary_function<_Arg, _Result>
10970b57cec5SDimitry Andric{
10980b57cec5SDimitry Andric    _Result (*__f_)(_Arg);
10990b57cec5SDimitry Andricpublic:
11000b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY explicit pointer_to_unary_function(_Result (*__f)(_Arg))
11010b57cec5SDimitry Andric        : __f_(__f) {}
11020b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY _Result operator()(_Arg __x) const
11030b57cec5SDimitry Andric        {return __f_(__x);}
11040b57cec5SDimitry Andric};
11050b57cec5SDimitry Andric
11060b57cec5SDimitry Andrictemplate <class _Arg, class _Result>
11070b57cec5SDimitry Andric_LIBCPP_DEPRECATED_IN_CXX11 inline _LIBCPP_INLINE_VISIBILITY
11080b57cec5SDimitry Andricpointer_to_unary_function<_Arg,_Result>
11090b57cec5SDimitry Andricptr_fun(_Result (*__f)(_Arg))
11100b57cec5SDimitry Andric    {return pointer_to_unary_function<_Arg,_Result>(__f);}
11110b57cec5SDimitry Andric
11120b57cec5SDimitry Andrictemplate <class _Arg1, class _Arg2, class _Result>
11130b57cec5SDimitry Andricclass _LIBCPP_TEMPLATE_VIS _LIBCPP_DEPRECATED_IN_CXX11 pointer_to_binary_function
11140b57cec5SDimitry Andric    : public binary_function<_Arg1, _Arg2, _Result>
11150b57cec5SDimitry Andric{
11160b57cec5SDimitry Andric    _Result (*__f_)(_Arg1, _Arg2);
11170b57cec5SDimitry Andricpublic:
11180b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY explicit pointer_to_binary_function(_Result (*__f)(_Arg1, _Arg2))
11190b57cec5SDimitry Andric        : __f_(__f) {}
11200b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY _Result operator()(_Arg1 __x, _Arg2 __y) const
11210b57cec5SDimitry Andric        {return __f_(__x, __y);}
11220b57cec5SDimitry Andric};
11230b57cec5SDimitry Andric
11240b57cec5SDimitry Andrictemplate <class _Arg1, class _Arg2, class _Result>
11250b57cec5SDimitry Andric_LIBCPP_DEPRECATED_IN_CXX11 inline _LIBCPP_INLINE_VISIBILITY
11260b57cec5SDimitry Andricpointer_to_binary_function<_Arg1,_Arg2,_Result>
11270b57cec5SDimitry Andricptr_fun(_Result (*__f)(_Arg1,_Arg2))
11280b57cec5SDimitry Andric    {return pointer_to_binary_function<_Arg1,_Arg2,_Result>(__f);}
11290b57cec5SDimitry Andric
11300b57cec5SDimitry Andrictemplate<class _Sp, class _Tp>
11310b57cec5SDimitry Andricclass _LIBCPP_TEMPLATE_VIS _LIBCPP_DEPRECATED_IN_CXX11 mem_fun_t
11320b57cec5SDimitry Andric    : public unary_function<_Tp*, _Sp>
11330b57cec5SDimitry Andric{
11340b57cec5SDimitry Andric    _Sp (_Tp::*__p_)();
11350b57cec5SDimitry Andricpublic:
11360b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY explicit mem_fun_t(_Sp (_Tp::*__p)())
11370b57cec5SDimitry Andric        : __p_(__p) {}
11380b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY _Sp operator()(_Tp* __p) const
11390b57cec5SDimitry Andric        {return (__p->*__p_)();}
11400b57cec5SDimitry Andric};
11410b57cec5SDimitry Andric
11420b57cec5SDimitry Andrictemplate<class _Sp, class _Tp, class _Ap>
11430b57cec5SDimitry Andricclass _LIBCPP_TEMPLATE_VIS _LIBCPP_DEPRECATED_IN_CXX11 mem_fun1_t
11440b57cec5SDimitry Andric    : public binary_function<_Tp*, _Ap, _Sp>
11450b57cec5SDimitry Andric{
11460b57cec5SDimitry Andric    _Sp (_Tp::*__p_)(_Ap);
11470b57cec5SDimitry Andricpublic:
11480b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY explicit mem_fun1_t(_Sp (_Tp::*__p)(_Ap))
11490b57cec5SDimitry Andric        : __p_(__p) {}
11500b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY _Sp operator()(_Tp* __p, _Ap __x) const
11510b57cec5SDimitry Andric        {return (__p->*__p_)(__x);}
11520b57cec5SDimitry Andric};
11530b57cec5SDimitry Andric
11540b57cec5SDimitry Andrictemplate<class _Sp, class _Tp>
11550b57cec5SDimitry Andric_LIBCPP_DEPRECATED_IN_CXX11 inline _LIBCPP_INLINE_VISIBILITY
11560b57cec5SDimitry Andricmem_fun_t<_Sp,_Tp>
11570b57cec5SDimitry Andricmem_fun(_Sp (_Tp::*__f)())
11580b57cec5SDimitry Andric    {return mem_fun_t<_Sp,_Tp>(__f);}
11590b57cec5SDimitry Andric
11600b57cec5SDimitry Andrictemplate<class _Sp, class _Tp, class _Ap>
11610b57cec5SDimitry Andric_LIBCPP_DEPRECATED_IN_CXX11 inline _LIBCPP_INLINE_VISIBILITY
11620b57cec5SDimitry Andricmem_fun1_t<_Sp,_Tp,_Ap>
11630b57cec5SDimitry Andricmem_fun(_Sp (_Tp::*__f)(_Ap))
11640b57cec5SDimitry Andric    {return mem_fun1_t<_Sp,_Tp,_Ap>(__f);}
11650b57cec5SDimitry Andric
11660b57cec5SDimitry Andrictemplate<class _Sp, class _Tp>
11670b57cec5SDimitry Andricclass _LIBCPP_TEMPLATE_VIS _LIBCPP_DEPRECATED_IN_CXX11 mem_fun_ref_t
11680b57cec5SDimitry Andric    : public unary_function<_Tp, _Sp>
11690b57cec5SDimitry Andric{
11700b57cec5SDimitry Andric    _Sp (_Tp::*__p_)();
11710b57cec5SDimitry Andricpublic:
11720b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY explicit mem_fun_ref_t(_Sp (_Tp::*__p)())
11730b57cec5SDimitry Andric        : __p_(__p) {}
11740b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY _Sp operator()(_Tp& __p) const
11750b57cec5SDimitry Andric        {return (__p.*__p_)();}
11760b57cec5SDimitry Andric};
11770b57cec5SDimitry Andric
11780b57cec5SDimitry Andrictemplate<class _Sp, class _Tp, class _Ap>
11790b57cec5SDimitry Andricclass _LIBCPP_TEMPLATE_VIS _LIBCPP_DEPRECATED_IN_CXX11 mem_fun1_ref_t
11800b57cec5SDimitry Andric    : public binary_function<_Tp, _Ap, _Sp>
11810b57cec5SDimitry Andric{
11820b57cec5SDimitry Andric    _Sp (_Tp::*__p_)(_Ap);
11830b57cec5SDimitry Andricpublic:
11840b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY explicit mem_fun1_ref_t(_Sp (_Tp::*__p)(_Ap))
11850b57cec5SDimitry Andric        : __p_(__p) {}
11860b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY _Sp operator()(_Tp& __p, _Ap __x) const
11870b57cec5SDimitry Andric        {return (__p.*__p_)(__x);}
11880b57cec5SDimitry Andric};
11890b57cec5SDimitry Andric
11900b57cec5SDimitry Andrictemplate<class _Sp, class _Tp>
11910b57cec5SDimitry Andric_LIBCPP_DEPRECATED_IN_CXX11 inline _LIBCPP_INLINE_VISIBILITY
11920b57cec5SDimitry Andricmem_fun_ref_t<_Sp,_Tp>
11930b57cec5SDimitry Andricmem_fun_ref(_Sp (_Tp::*__f)())
11940b57cec5SDimitry Andric    {return mem_fun_ref_t<_Sp,_Tp>(__f);}
11950b57cec5SDimitry Andric
11960b57cec5SDimitry Andrictemplate<class _Sp, class _Tp, class _Ap>
11970b57cec5SDimitry Andric_LIBCPP_DEPRECATED_IN_CXX11 inline _LIBCPP_INLINE_VISIBILITY
11980b57cec5SDimitry Andricmem_fun1_ref_t<_Sp,_Tp,_Ap>
11990b57cec5SDimitry Andricmem_fun_ref(_Sp (_Tp::*__f)(_Ap))
12000b57cec5SDimitry Andric    {return mem_fun1_ref_t<_Sp,_Tp,_Ap>(__f);}
12010b57cec5SDimitry Andric
12020b57cec5SDimitry Andrictemplate <class _Sp, class _Tp>
12030b57cec5SDimitry Andricclass _LIBCPP_TEMPLATE_VIS _LIBCPP_DEPRECATED_IN_CXX11 const_mem_fun_t
12040b57cec5SDimitry Andric    : public unary_function<const _Tp*, _Sp>
12050b57cec5SDimitry Andric{
12060b57cec5SDimitry Andric    _Sp (_Tp::*__p_)() const;
12070b57cec5SDimitry Andricpublic:
12080b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY explicit const_mem_fun_t(_Sp (_Tp::*__p)() const)
12090b57cec5SDimitry Andric        : __p_(__p) {}
12100b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY _Sp operator()(const _Tp* __p) const
12110b57cec5SDimitry Andric        {return (__p->*__p_)();}
12120b57cec5SDimitry Andric};
12130b57cec5SDimitry Andric
12140b57cec5SDimitry Andrictemplate <class _Sp, class _Tp, class _Ap>
12150b57cec5SDimitry Andricclass _LIBCPP_TEMPLATE_VIS _LIBCPP_DEPRECATED_IN_CXX11 const_mem_fun1_t
12160b57cec5SDimitry Andric    : public binary_function<const _Tp*, _Ap, _Sp>
12170b57cec5SDimitry Andric{
12180b57cec5SDimitry Andric    _Sp (_Tp::*__p_)(_Ap) const;
12190b57cec5SDimitry Andricpublic:
12200b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY explicit const_mem_fun1_t(_Sp (_Tp::*__p)(_Ap) const)
12210b57cec5SDimitry Andric        : __p_(__p) {}
12220b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY _Sp operator()(const _Tp* __p, _Ap __x) const
12230b57cec5SDimitry Andric        {return (__p->*__p_)(__x);}
12240b57cec5SDimitry Andric};
12250b57cec5SDimitry Andric
12260b57cec5SDimitry Andrictemplate <class _Sp, class _Tp>
12270b57cec5SDimitry Andric_LIBCPP_DEPRECATED_IN_CXX11 inline _LIBCPP_INLINE_VISIBILITY
12280b57cec5SDimitry Andricconst_mem_fun_t<_Sp,_Tp>
12290b57cec5SDimitry Andricmem_fun(_Sp (_Tp::*__f)() const)
12300b57cec5SDimitry Andric    {return const_mem_fun_t<_Sp,_Tp>(__f);}
12310b57cec5SDimitry Andric
12320b57cec5SDimitry Andrictemplate <class _Sp, class _Tp, class _Ap>
12330b57cec5SDimitry Andric_LIBCPP_DEPRECATED_IN_CXX11 inline _LIBCPP_INLINE_VISIBILITY
12340b57cec5SDimitry Andricconst_mem_fun1_t<_Sp,_Tp,_Ap>
12350b57cec5SDimitry Andricmem_fun(_Sp (_Tp::*__f)(_Ap) const)
12360b57cec5SDimitry Andric    {return const_mem_fun1_t<_Sp,_Tp,_Ap>(__f);}
12370b57cec5SDimitry Andric
12380b57cec5SDimitry Andrictemplate <class _Sp, class _Tp>
12390b57cec5SDimitry Andricclass _LIBCPP_TEMPLATE_VIS _LIBCPP_DEPRECATED_IN_CXX11 const_mem_fun_ref_t
12400b57cec5SDimitry Andric    : public unary_function<_Tp, _Sp>
12410b57cec5SDimitry Andric{
12420b57cec5SDimitry Andric    _Sp (_Tp::*__p_)() const;
12430b57cec5SDimitry Andricpublic:
12440b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY explicit const_mem_fun_ref_t(_Sp (_Tp::*__p)() const)
12450b57cec5SDimitry Andric        : __p_(__p) {}
12460b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY _Sp operator()(const _Tp& __p) const
12470b57cec5SDimitry Andric        {return (__p.*__p_)();}
12480b57cec5SDimitry Andric};
12490b57cec5SDimitry Andric
12500b57cec5SDimitry Andrictemplate <class _Sp, class _Tp, class _Ap>
12510b57cec5SDimitry Andricclass _LIBCPP_TEMPLATE_VIS _LIBCPP_DEPRECATED_IN_CXX11 const_mem_fun1_ref_t
12520b57cec5SDimitry Andric    : public binary_function<_Tp, _Ap, _Sp>
12530b57cec5SDimitry Andric{
12540b57cec5SDimitry Andric    _Sp (_Tp::*__p_)(_Ap) const;
12550b57cec5SDimitry Andricpublic:
12560b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY explicit const_mem_fun1_ref_t(_Sp (_Tp::*__p)(_Ap) const)
12570b57cec5SDimitry Andric        : __p_(__p) {}
12580b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY _Sp operator()(const _Tp& __p, _Ap __x) const
12590b57cec5SDimitry Andric        {return (__p.*__p_)(__x);}
12600b57cec5SDimitry Andric};
12610b57cec5SDimitry Andric
12620b57cec5SDimitry Andrictemplate <class _Sp, class _Tp>
12630b57cec5SDimitry Andric_LIBCPP_DEPRECATED_IN_CXX11 inline _LIBCPP_INLINE_VISIBILITY
12640b57cec5SDimitry Andricconst_mem_fun_ref_t<_Sp,_Tp>
12650b57cec5SDimitry Andricmem_fun_ref(_Sp (_Tp::*__f)() const)
12660b57cec5SDimitry Andric    {return const_mem_fun_ref_t<_Sp,_Tp>(__f);}
12670b57cec5SDimitry Andric
12680b57cec5SDimitry Andrictemplate <class _Sp, class _Tp, class _Ap>
12690b57cec5SDimitry Andric_LIBCPP_DEPRECATED_IN_CXX11 inline _LIBCPP_INLINE_VISIBILITY
12700b57cec5SDimitry Andricconst_mem_fun1_ref_t<_Sp,_Tp,_Ap>
12710b57cec5SDimitry Andricmem_fun_ref(_Sp (_Tp::*__f)(_Ap) const)
12720b57cec5SDimitry Andric    {return const_mem_fun1_ref_t<_Sp,_Tp,_Ap>(__f);}
12730b57cec5SDimitry Andric#endif
12740b57cec5SDimitry Andric
12750b57cec5SDimitry Andric////////////////////////////////////////////////////////////////////////////////
12760b57cec5SDimitry Andric//                                MEMFUN
12770b57cec5SDimitry Andric//==============================================================================
12780b57cec5SDimitry Andric
12790b57cec5SDimitry Andrictemplate <class _Tp>
12800b57cec5SDimitry Andricclass __mem_fn
12810b57cec5SDimitry Andric    : public __weak_result_type<_Tp>
12820b57cec5SDimitry Andric{
12830b57cec5SDimitry Andricpublic:
12840b57cec5SDimitry Andric    // types
12850b57cec5SDimitry Andric    typedef _Tp type;
12860b57cec5SDimitry Andricprivate:
12870b57cec5SDimitry Andric    type __f_;
12880b57cec5SDimitry Andric
12890b57cec5SDimitry Andricpublic:
12900b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY __mem_fn(type __f) _NOEXCEPT : __f_(__f) {}
12910b57cec5SDimitry Andric
12920b57cec5SDimitry Andric#ifndef _LIBCPP_CXX03_LANG
12930b57cec5SDimitry Andric    // invoke
12940b57cec5SDimitry Andric    template <class... _ArgTypes>
12950b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
12960b57cec5SDimitry Andric    typename __invoke_return<type, _ArgTypes...>::type
12970b57cec5SDimitry Andric    operator() (_ArgTypes&&... __args) const {
12980b57cec5SDimitry Andric        return __invoke(__f_, _VSTD::forward<_ArgTypes>(__args)...);
12990b57cec5SDimitry Andric    }
13000b57cec5SDimitry Andric#else
13010b57cec5SDimitry Andric
13020b57cec5SDimitry Andric    template <class _A0>
13030b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
13040b57cec5SDimitry Andric    typename __invoke_return0<type, _A0>::type
13050b57cec5SDimitry Andric    operator() (_A0& __a0) const {
13060b57cec5SDimitry Andric        return __invoke(__f_, __a0);
13070b57cec5SDimitry Andric    }
13080b57cec5SDimitry Andric
13090b57cec5SDimitry Andric    template <class _A0>
13100b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
13110b57cec5SDimitry Andric    typename __invoke_return0<type, _A0 const>::type
13120b57cec5SDimitry Andric    operator() (_A0 const& __a0) const {
13130b57cec5SDimitry Andric        return __invoke(__f_, __a0);
13140b57cec5SDimitry Andric    }
13150b57cec5SDimitry Andric
13160b57cec5SDimitry Andric    template <class _A0, class _A1>
13170b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
13180b57cec5SDimitry Andric    typename __invoke_return1<type, _A0, _A1>::type
13190b57cec5SDimitry Andric    operator() (_A0& __a0, _A1& __a1) const {
13200b57cec5SDimitry Andric        return __invoke(__f_, __a0, __a1);
13210b57cec5SDimitry Andric    }
13220b57cec5SDimitry Andric
13230b57cec5SDimitry Andric    template <class _A0, class _A1>
13240b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
13250b57cec5SDimitry Andric    typename __invoke_return1<type, _A0 const, _A1>::type
13260b57cec5SDimitry Andric    operator() (_A0 const& __a0, _A1& __a1) const {
13270b57cec5SDimitry Andric        return __invoke(__f_, __a0, __a1);
13280b57cec5SDimitry Andric    }
13290b57cec5SDimitry Andric
13300b57cec5SDimitry Andric    template <class _A0, class _A1>
13310b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
13320b57cec5SDimitry Andric    typename __invoke_return1<type, _A0, _A1 const>::type
13330b57cec5SDimitry Andric    operator() (_A0& __a0, _A1 const& __a1) const {
13340b57cec5SDimitry Andric        return __invoke(__f_, __a0, __a1);
13350b57cec5SDimitry Andric    }
13360b57cec5SDimitry Andric
13370b57cec5SDimitry Andric    template <class _A0, class _A1>
13380b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
13390b57cec5SDimitry Andric    typename __invoke_return1<type, _A0 const, _A1 const>::type
13400b57cec5SDimitry Andric    operator() (_A0 const& __a0, _A1 const& __a1) const {
13410b57cec5SDimitry Andric        return __invoke(__f_, __a0, __a1);
13420b57cec5SDimitry Andric    }
13430b57cec5SDimitry Andric
13440b57cec5SDimitry Andric    template <class _A0, class _A1, class _A2>
13450b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
13460b57cec5SDimitry Andric    typename __invoke_return2<type, _A0, _A1, _A2>::type
13470b57cec5SDimitry Andric    operator() (_A0& __a0, _A1& __a1, _A2& __a2) const {
13480b57cec5SDimitry Andric        return __invoke(__f_, __a0, __a1, __a2);
13490b57cec5SDimitry Andric    }
13500b57cec5SDimitry Andric
13510b57cec5SDimitry Andric    template <class _A0, class _A1, class _A2>
13520b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
13530b57cec5SDimitry Andric    typename __invoke_return2<type, _A0 const, _A1, _A2>::type
13540b57cec5SDimitry Andric    operator() (_A0 const& __a0, _A1& __a1, _A2& __a2) const {
13550b57cec5SDimitry Andric        return __invoke(__f_, __a0, __a1, __a2);
13560b57cec5SDimitry Andric    }
13570b57cec5SDimitry Andric
13580b57cec5SDimitry Andric    template <class _A0, class _A1, class _A2>
13590b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
13600b57cec5SDimitry Andric    typename __invoke_return2<type, _A0, _A1 const, _A2>::type
13610b57cec5SDimitry Andric    operator() (_A0& __a0, _A1 const& __a1, _A2& __a2) const {
13620b57cec5SDimitry Andric        return __invoke(__f_, __a0, __a1, __a2);
13630b57cec5SDimitry Andric    }
13640b57cec5SDimitry Andric
13650b57cec5SDimitry Andric    template <class _A0, class _A1, class _A2>
13660b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
13670b57cec5SDimitry Andric    typename __invoke_return2<type, _A0, _A1, _A2 const>::type
13680b57cec5SDimitry Andric    operator() (_A0& __a0, _A1& __a1, _A2 const& __a2) const {
13690b57cec5SDimitry Andric        return __invoke(__f_, __a0, __a1, __a2);
13700b57cec5SDimitry Andric    }
13710b57cec5SDimitry Andric
13720b57cec5SDimitry Andric    template <class _A0, class _A1, class _A2>
13730b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
13740b57cec5SDimitry Andric    typename __invoke_return2<type, _A0 const, _A1 const, _A2>::type
13750b57cec5SDimitry Andric    operator() (_A0 const& __a0, _A1 const& __a1, _A2& __a2) const {
13760b57cec5SDimitry Andric        return __invoke(__f_, __a0, __a1, __a2);
13770b57cec5SDimitry Andric    }
13780b57cec5SDimitry Andric
13790b57cec5SDimitry Andric    template <class _A0, class _A1, class _A2>
13800b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
13810b57cec5SDimitry Andric    typename __invoke_return2<type, _A0 const, _A1, _A2 const>::type
13820b57cec5SDimitry Andric    operator() (_A0 const& __a0, _A1& __a1, _A2 const& __a2) const {
13830b57cec5SDimitry Andric        return __invoke(__f_, __a0, __a1, __a2);
13840b57cec5SDimitry Andric    }
13850b57cec5SDimitry Andric
13860b57cec5SDimitry Andric    template <class _A0, class _A1, class _A2>
13870b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
13880b57cec5SDimitry Andric    typename __invoke_return2<type, _A0, _A1 const, _A2 const>::type
13890b57cec5SDimitry Andric    operator() (_A0& __a0, _A1 const& __a1, _A2 const& __a2) const {
13900b57cec5SDimitry Andric        return __invoke(__f_, __a0, __a1, __a2);
13910b57cec5SDimitry Andric    }
13920b57cec5SDimitry Andric
13930b57cec5SDimitry Andric    template <class _A0, class _A1, class _A2>
13940b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
13950b57cec5SDimitry Andric    typename __invoke_return2<type, _A0 const, _A1 const, _A2 const>::type
13960b57cec5SDimitry Andric    operator() (_A0 const& __a0, _A1 const& __a1, _A2 const& __a2) const {
13970b57cec5SDimitry Andric        return __invoke(__f_, __a0, __a1, __a2);
13980b57cec5SDimitry Andric    }
13990b57cec5SDimitry Andric#endif
14000b57cec5SDimitry Andric};
14010b57cec5SDimitry Andric
14020b57cec5SDimitry Andrictemplate<class _Rp, class _Tp>
14030b57cec5SDimitry Andricinline _LIBCPP_INLINE_VISIBILITY
14040b57cec5SDimitry Andric__mem_fn<_Rp _Tp::*>
14050b57cec5SDimitry Andricmem_fn(_Rp _Tp::* __pm) _NOEXCEPT
14060b57cec5SDimitry Andric{
14070b57cec5SDimitry Andric    return __mem_fn<_Rp _Tp::*>(__pm);
14080b57cec5SDimitry Andric}
14090b57cec5SDimitry Andric
14100b57cec5SDimitry Andric////////////////////////////////////////////////////////////////////////////////
14110b57cec5SDimitry Andric//                                FUNCTION
14120b57cec5SDimitry Andric//==============================================================================
14130b57cec5SDimitry Andric
14140b57cec5SDimitry Andric// bad_function_call
14150b57cec5SDimitry Andric
14160b57cec5SDimitry Andricclass _LIBCPP_EXCEPTION_ABI bad_function_call
14170b57cec5SDimitry Andric    : public exception
14180b57cec5SDimitry Andric{
14190b57cec5SDimitry Andric#ifdef _LIBCPP_ABI_BAD_FUNCTION_CALL_KEY_FUNCTION
14200b57cec5SDimitry Andricpublic:
14210b57cec5SDimitry Andric    virtual ~bad_function_call() _NOEXCEPT;
14220b57cec5SDimitry Andric
14230b57cec5SDimitry Andric    virtual const char* what() const _NOEXCEPT;
14240b57cec5SDimitry Andric#endif
14250b57cec5SDimitry Andric};
14260b57cec5SDimitry Andric
14270b57cec5SDimitry Andric_LIBCPP_NORETURN inline _LIBCPP_INLINE_VISIBILITY
14280b57cec5SDimitry Andricvoid __throw_bad_function_call()
14290b57cec5SDimitry Andric{
14300b57cec5SDimitry Andric#ifndef _LIBCPP_NO_EXCEPTIONS
14310b57cec5SDimitry Andric    throw bad_function_call();
14320b57cec5SDimitry Andric#else
14330b57cec5SDimitry Andric    _VSTD::abort();
14340b57cec5SDimitry Andric#endif
14350b57cec5SDimitry Andric}
14360b57cec5SDimitry Andric
14370b57cec5SDimitry Andrictemplate<class _Fp> class _LIBCPP_TEMPLATE_VIS function; // undefined
14380b57cec5SDimitry Andric
14390b57cec5SDimitry Andricnamespace __function
14400b57cec5SDimitry Andric{
14410b57cec5SDimitry Andric
14420b57cec5SDimitry Andrictemplate<class _Rp>
14430b57cec5SDimitry Andricstruct __maybe_derive_from_unary_function
14440b57cec5SDimitry Andric{
14450b57cec5SDimitry Andric};
14460b57cec5SDimitry Andric
14470b57cec5SDimitry Andrictemplate<class _Rp, class _A1>
14480b57cec5SDimitry Andricstruct __maybe_derive_from_unary_function<_Rp(_A1)>
14490b57cec5SDimitry Andric    : public unary_function<_A1, _Rp>
14500b57cec5SDimitry Andric{
14510b57cec5SDimitry Andric};
14520b57cec5SDimitry Andric
14530b57cec5SDimitry Andrictemplate<class _Rp>
14540b57cec5SDimitry Andricstruct __maybe_derive_from_binary_function
14550b57cec5SDimitry Andric{
14560b57cec5SDimitry Andric};
14570b57cec5SDimitry Andric
14580b57cec5SDimitry Andrictemplate<class _Rp, class _A1, class _A2>
14590b57cec5SDimitry Andricstruct __maybe_derive_from_binary_function<_Rp(_A1, _A2)>
14600b57cec5SDimitry Andric    : public binary_function<_A1, _A2, _Rp>
14610b57cec5SDimitry Andric{
14620b57cec5SDimitry Andric};
14630b57cec5SDimitry Andric
14640b57cec5SDimitry Andrictemplate <class _Fp>
14650b57cec5SDimitry Andric_LIBCPP_INLINE_VISIBILITY
14660b57cec5SDimitry Andricbool __not_null(_Fp const&) { return true; }
14670b57cec5SDimitry Andric
14680b57cec5SDimitry Andrictemplate <class _Fp>
14690b57cec5SDimitry Andric_LIBCPP_INLINE_VISIBILITY
14700b57cec5SDimitry Andricbool __not_null(_Fp* __ptr) { return __ptr; }
14710b57cec5SDimitry Andric
14720b57cec5SDimitry Andrictemplate <class _Ret, class _Class>
14730b57cec5SDimitry Andric_LIBCPP_INLINE_VISIBILITY
14740b57cec5SDimitry Andricbool __not_null(_Ret _Class::*__ptr) { return __ptr; }
14750b57cec5SDimitry Andric
14760b57cec5SDimitry Andrictemplate <class _Fp>
14770b57cec5SDimitry Andric_LIBCPP_INLINE_VISIBILITY
14780b57cec5SDimitry Andricbool __not_null(function<_Fp> const& __f) { return !!__f; }
14790b57cec5SDimitry Andric
14800b57cec5SDimitry Andric} // namespace __function
14810b57cec5SDimitry Andric
14820b57cec5SDimitry Andric#ifndef _LIBCPP_CXX03_LANG
14830b57cec5SDimitry Andric
14840b57cec5SDimitry Andricnamespace __function {
14850b57cec5SDimitry Andric
14860b57cec5SDimitry Andric// __alloc_func holds a functor and an allocator.
14870b57cec5SDimitry Andric
14880b57cec5SDimitry Andrictemplate <class _Fp, class _Ap, class _FB> class __alloc_func;
14890b57cec5SDimitry Andrictemplate <class _Fp, class _FB>
14900b57cec5SDimitry Andricclass __default_alloc_func;
14910b57cec5SDimitry Andric
14920b57cec5SDimitry Andrictemplate <class _Fp, class _Ap, class _Rp, class... _ArgTypes>
14930b57cec5SDimitry Andricclass __alloc_func<_Fp, _Ap, _Rp(_ArgTypes...)>
14940b57cec5SDimitry Andric{
14950b57cec5SDimitry Andric    __compressed_pair<_Fp, _Ap> __f_;
14960b57cec5SDimitry Andric
14970b57cec5SDimitry Andric  public:
14980b57cec5SDimitry Andric    typedef _LIBCPP_NODEBUG_TYPE _Fp _Target;
14990b57cec5SDimitry Andric    typedef _LIBCPP_NODEBUG_TYPE _Ap _Alloc;
15000b57cec5SDimitry Andric
15010b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
15020b57cec5SDimitry Andric    const _Target& __target() const { return __f_.first(); }
15030b57cec5SDimitry Andric
15040b57cec5SDimitry Andric    // WIN32 APIs may define __allocator, so use __get_allocator instead.
15050b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
15060b57cec5SDimitry Andric    const _Alloc& __get_allocator() const { return __f_.second(); }
15070b57cec5SDimitry Andric
15080b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
15090b57cec5SDimitry Andric    explicit __alloc_func(_Target&& __f)
15100b57cec5SDimitry Andric        : __f_(piecewise_construct, _VSTD::forward_as_tuple(_VSTD::move(__f)),
15110b57cec5SDimitry Andric               _VSTD::forward_as_tuple())
15120b57cec5SDimitry Andric    {
15130b57cec5SDimitry Andric    }
15140b57cec5SDimitry Andric
15150b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
15160b57cec5SDimitry Andric    explicit __alloc_func(const _Target& __f, const _Alloc& __a)
15170b57cec5SDimitry Andric        : __f_(piecewise_construct, _VSTD::forward_as_tuple(__f),
15180b57cec5SDimitry Andric               _VSTD::forward_as_tuple(__a))
15190b57cec5SDimitry Andric    {
15200b57cec5SDimitry Andric    }
15210b57cec5SDimitry Andric
15220b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
15230b57cec5SDimitry Andric    explicit __alloc_func(const _Target& __f, _Alloc&& __a)
15240b57cec5SDimitry Andric        : __f_(piecewise_construct, _VSTD::forward_as_tuple(__f),
15250b57cec5SDimitry Andric               _VSTD::forward_as_tuple(_VSTD::move(__a)))
15260b57cec5SDimitry Andric    {
15270b57cec5SDimitry Andric    }
15280b57cec5SDimitry Andric
15290b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
15300b57cec5SDimitry Andric    explicit __alloc_func(_Target&& __f, _Alloc&& __a)
15310b57cec5SDimitry Andric        : __f_(piecewise_construct, _VSTD::forward_as_tuple(_VSTD::move(__f)),
15320b57cec5SDimitry Andric               _VSTD::forward_as_tuple(_VSTD::move(__a)))
15330b57cec5SDimitry Andric    {
15340b57cec5SDimitry Andric    }
15350b57cec5SDimitry Andric
15360b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
15370b57cec5SDimitry Andric    _Rp operator()(_ArgTypes&&... __arg)
15380b57cec5SDimitry Andric    {
15390b57cec5SDimitry Andric        typedef __invoke_void_return_wrapper<_Rp> _Invoker;
15400b57cec5SDimitry Andric        return _Invoker::__call(__f_.first(),
15410b57cec5SDimitry Andric                                _VSTD::forward<_ArgTypes>(__arg)...);
15420b57cec5SDimitry Andric    }
15430b57cec5SDimitry Andric
15440b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
15450b57cec5SDimitry Andric    __alloc_func* __clone() const
15460b57cec5SDimitry Andric    {
15470b57cec5SDimitry Andric        typedef allocator_traits<_Alloc> __alloc_traits;
15480b57cec5SDimitry Andric        typedef
15490b57cec5SDimitry Andric            typename __rebind_alloc_helper<__alloc_traits, __alloc_func>::type
15500b57cec5SDimitry Andric                _AA;
15510b57cec5SDimitry Andric        _AA __a(__f_.second());
15520b57cec5SDimitry Andric        typedef __allocator_destructor<_AA> _Dp;
15530b57cec5SDimitry Andric        unique_ptr<__alloc_func, _Dp> __hold(__a.allocate(1), _Dp(__a, 1));
15540b57cec5SDimitry Andric        ::new ((void*)__hold.get()) __alloc_func(__f_.first(), _Alloc(__a));
15550b57cec5SDimitry Andric        return __hold.release();
15560b57cec5SDimitry Andric    }
15570b57cec5SDimitry Andric
15580b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
15590b57cec5SDimitry Andric    void destroy() _NOEXCEPT { __f_.~__compressed_pair<_Target, _Alloc>(); }
15600b57cec5SDimitry Andric
15610b57cec5SDimitry Andric    static void __destroy_and_delete(__alloc_func* __f) {
15620b57cec5SDimitry Andric      typedef allocator_traits<_Alloc> __alloc_traits;
15630b57cec5SDimitry Andric      typedef typename __rebind_alloc_helper<__alloc_traits, __alloc_func>::type
15640b57cec5SDimitry Andric          _FunAlloc;
15650b57cec5SDimitry Andric      _FunAlloc __a(__f->__get_allocator());
15660b57cec5SDimitry Andric      __f->destroy();
15670b57cec5SDimitry Andric      __a.deallocate(__f, 1);
15680b57cec5SDimitry Andric    }
15690b57cec5SDimitry Andric};
15700b57cec5SDimitry Andric
15710b57cec5SDimitry Andrictemplate <class _Fp, class _Rp, class... _ArgTypes>
15720b57cec5SDimitry Andricclass __default_alloc_func<_Fp, _Rp(_ArgTypes...)> {
15730b57cec5SDimitry Andric  _Fp __f_;
15740b57cec5SDimitry Andric
15750b57cec5SDimitry Andricpublic:
15760b57cec5SDimitry Andric  typedef _LIBCPP_NODEBUG_TYPE _Fp _Target;
15770b57cec5SDimitry Andric
15780b57cec5SDimitry Andric  _LIBCPP_INLINE_VISIBILITY
15790b57cec5SDimitry Andric  const _Target& __target() const { return __f_; }
15800b57cec5SDimitry Andric
15810b57cec5SDimitry Andric  _LIBCPP_INLINE_VISIBILITY
15820b57cec5SDimitry Andric  explicit __default_alloc_func(_Target&& __f) : __f_(std::move(__f)) {}
15830b57cec5SDimitry Andric
15840b57cec5SDimitry Andric  _LIBCPP_INLINE_VISIBILITY
15850b57cec5SDimitry Andric  explicit __default_alloc_func(const _Target& __f) : __f_(__f) {}
15860b57cec5SDimitry Andric
15870b57cec5SDimitry Andric  _LIBCPP_INLINE_VISIBILITY
15880b57cec5SDimitry Andric  _Rp operator()(_ArgTypes&&... __arg) {
15890b57cec5SDimitry Andric    typedef __invoke_void_return_wrapper<_Rp> _Invoker;
15900b57cec5SDimitry Andric    return _Invoker::__call(__f_, _VSTD::forward<_ArgTypes>(__arg)...);
15910b57cec5SDimitry Andric  }
15920b57cec5SDimitry Andric
15930b57cec5SDimitry Andric  _LIBCPP_INLINE_VISIBILITY
15940b57cec5SDimitry Andric  __default_alloc_func* __clone() const {
15950b57cec5SDimitry Andric      __builtin_new_allocator::__holder_t __hold =
15960b57cec5SDimitry Andric        __builtin_new_allocator::__allocate_type<__default_alloc_func>(1);
15970b57cec5SDimitry Andric    __default_alloc_func* __res =
15980b57cec5SDimitry Andric        ::new (__hold.get()) __default_alloc_func(__f_);
15990b57cec5SDimitry Andric    (void)__hold.release();
16000b57cec5SDimitry Andric    return __res;
16010b57cec5SDimitry Andric  }
16020b57cec5SDimitry Andric
16030b57cec5SDimitry Andric  _LIBCPP_INLINE_VISIBILITY
16040b57cec5SDimitry Andric  void destroy() _NOEXCEPT { __f_.~_Target(); }
16050b57cec5SDimitry Andric
16060b57cec5SDimitry Andric  static void __destroy_and_delete(__default_alloc_func* __f) {
16070b57cec5SDimitry Andric    __f->destroy();
16080b57cec5SDimitry Andric      __builtin_new_allocator::__deallocate_type<__default_alloc_func>(__f, 1);
16090b57cec5SDimitry Andric  }
16100b57cec5SDimitry Andric};
16110b57cec5SDimitry Andric
16120b57cec5SDimitry Andric// __base provides an abstract interface for copyable functors.
16130b57cec5SDimitry Andric
16140b57cec5SDimitry Andrictemplate<class _Fp> class __base;
16150b57cec5SDimitry Andric
16160b57cec5SDimitry Andrictemplate<class _Rp, class ..._ArgTypes>
16170b57cec5SDimitry Andricclass __base<_Rp(_ArgTypes...)>
16180b57cec5SDimitry Andric{
16190b57cec5SDimitry Andric    __base(const __base&);
16200b57cec5SDimitry Andric    __base& operator=(const __base&);
16210b57cec5SDimitry Andricpublic:
16220b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY __base() {}
16230b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY virtual ~__base() {}
16240b57cec5SDimitry Andric    virtual __base* __clone() const = 0;
16250b57cec5SDimitry Andric    virtual void __clone(__base*) const = 0;
16260b57cec5SDimitry Andric    virtual void destroy() _NOEXCEPT = 0;
16270b57cec5SDimitry Andric    virtual void destroy_deallocate() _NOEXCEPT = 0;
16280b57cec5SDimitry Andric    virtual _Rp operator()(_ArgTypes&& ...) = 0;
16290b57cec5SDimitry Andric#ifndef _LIBCPP_NO_RTTI
16300b57cec5SDimitry Andric    virtual const void* target(const type_info&) const _NOEXCEPT = 0;
16310b57cec5SDimitry Andric    virtual const std::type_info& target_type() const _NOEXCEPT = 0;
16320b57cec5SDimitry Andric#endif  // _LIBCPP_NO_RTTI
16330b57cec5SDimitry Andric};
16340b57cec5SDimitry Andric
16350b57cec5SDimitry Andric// __func implements __base for a given functor type.
16360b57cec5SDimitry Andric
16370b57cec5SDimitry Andrictemplate<class _FD, class _Alloc, class _FB> class __func;
16380b57cec5SDimitry Andric
16390b57cec5SDimitry Andrictemplate<class _Fp, class _Alloc, class _Rp, class ..._ArgTypes>
16400b57cec5SDimitry Andricclass __func<_Fp, _Alloc, _Rp(_ArgTypes...)>
16410b57cec5SDimitry Andric    : public  __base<_Rp(_ArgTypes...)>
16420b57cec5SDimitry Andric{
16430b57cec5SDimitry Andric    __alloc_func<_Fp, _Alloc, _Rp(_ArgTypes...)> __f_;
16440b57cec5SDimitry Andricpublic:
16450b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
16460b57cec5SDimitry Andric    explicit __func(_Fp&& __f)
16470b57cec5SDimitry Andric        : __f_(_VSTD::move(__f)) {}
16480b57cec5SDimitry Andric
16490b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
16500b57cec5SDimitry Andric    explicit __func(const _Fp& __f, const _Alloc& __a)
16510b57cec5SDimitry Andric        : __f_(__f, __a) {}
16520b57cec5SDimitry Andric
16530b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
16540b57cec5SDimitry Andric    explicit __func(const _Fp& __f, _Alloc&& __a)
16550b57cec5SDimitry Andric        : __f_(__f, _VSTD::move(__a)) {}
16560b57cec5SDimitry Andric
16570b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
16580b57cec5SDimitry Andric    explicit __func(_Fp&& __f, _Alloc&& __a)
16590b57cec5SDimitry Andric        : __f_(_VSTD::move(__f), _VSTD::move(__a)) {}
16600b57cec5SDimitry Andric
16610b57cec5SDimitry Andric    virtual __base<_Rp(_ArgTypes...)>* __clone() const;
16620b57cec5SDimitry Andric    virtual void __clone(__base<_Rp(_ArgTypes...)>*) const;
16630b57cec5SDimitry Andric    virtual void destroy() _NOEXCEPT;
16640b57cec5SDimitry Andric    virtual void destroy_deallocate() _NOEXCEPT;
16650b57cec5SDimitry Andric    virtual _Rp operator()(_ArgTypes&&... __arg);
16660b57cec5SDimitry Andric#ifndef _LIBCPP_NO_RTTI
16670b57cec5SDimitry Andric    virtual const void* target(const type_info&) const _NOEXCEPT;
16680b57cec5SDimitry Andric    virtual const std::type_info& target_type() const _NOEXCEPT;
16690b57cec5SDimitry Andric#endif  // _LIBCPP_NO_RTTI
16700b57cec5SDimitry Andric};
16710b57cec5SDimitry Andric
16720b57cec5SDimitry Andrictemplate<class _Fp, class _Alloc, class _Rp, class ..._ArgTypes>
16730b57cec5SDimitry Andric__base<_Rp(_ArgTypes...)>*
16740b57cec5SDimitry Andric__func<_Fp, _Alloc, _Rp(_ArgTypes...)>::__clone() const
16750b57cec5SDimitry Andric{
16760b57cec5SDimitry Andric    typedef allocator_traits<_Alloc> __alloc_traits;
16770b57cec5SDimitry Andric    typedef typename __rebind_alloc_helper<__alloc_traits, __func>::type _Ap;
16780b57cec5SDimitry Andric    _Ap __a(__f_.__get_allocator());
16790b57cec5SDimitry Andric    typedef __allocator_destructor<_Ap> _Dp;
16800b57cec5SDimitry Andric    unique_ptr<__func, _Dp> __hold(__a.allocate(1), _Dp(__a, 1));
16810b57cec5SDimitry Andric    ::new ((void*)__hold.get()) __func(__f_.__target(), _Alloc(__a));
16820b57cec5SDimitry Andric    return __hold.release();
16830b57cec5SDimitry Andric}
16840b57cec5SDimitry Andric
16850b57cec5SDimitry Andrictemplate<class _Fp, class _Alloc, class _Rp, class ..._ArgTypes>
16860b57cec5SDimitry Andricvoid
16870b57cec5SDimitry Andric__func<_Fp, _Alloc, _Rp(_ArgTypes...)>::__clone(__base<_Rp(_ArgTypes...)>* __p) const
16880b57cec5SDimitry Andric{
16890b57cec5SDimitry Andric    ::new (__p) __func(__f_.__target(), __f_.__get_allocator());
16900b57cec5SDimitry Andric}
16910b57cec5SDimitry Andric
16920b57cec5SDimitry Andrictemplate<class _Fp, class _Alloc, class _Rp, class ..._ArgTypes>
16930b57cec5SDimitry Andricvoid
16940b57cec5SDimitry Andric__func<_Fp, _Alloc, _Rp(_ArgTypes...)>::destroy() _NOEXCEPT
16950b57cec5SDimitry Andric{
16960b57cec5SDimitry Andric    __f_.destroy();
16970b57cec5SDimitry Andric}
16980b57cec5SDimitry Andric
16990b57cec5SDimitry Andrictemplate<class _Fp, class _Alloc, class _Rp, class ..._ArgTypes>
17000b57cec5SDimitry Andricvoid
17010b57cec5SDimitry Andric__func<_Fp, _Alloc, _Rp(_ArgTypes...)>::destroy_deallocate() _NOEXCEPT
17020b57cec5SDimitry Andric{
17030b57cec5SDimitry Andric    typedef allocator_traits<_Alloc> __alloc_traits;
17040b57cec5SDimitry Andric    typedef typename __rebind_alloc_helper<__alloc_traits, __func>::type _Ap;
17050b57cec5SDimitry Andric    _Ap __a(__f_.__get_allocator());
17060b57cec5SDimitry Andric    __f_.destroy();
17070b57cec5SDimitry Andric    __a.deallocate(this, 1);
17080b57cec5SDimitry Andric}
17090b57cec5SDimitry Andric
17100b57cec5SDimitry Andrictemplate<class _Fp, class _Alloc, class _Rp, class ..._ArgTypes>
17110b57cec5SDimitry Andric_Rp
17120b57cec5SDimitry Andric__func<_Fp, _Alloc, _Rp(_ArgTypes...)>::operator()(_ArgTypes&& ... __arg)
17130b57cec5SDimitry Andric{
17140b57cec5SDimitry Andric    return __f_(_VSTD::forward<_ArgTypes>(__arg)...);
17150b57cec5SDimitry Andric}
17160b57cec5SDimitry Andric
17170b57cec5SDimitry Andric#ifndef _LIBCPP_NO_RTTI
17180b57cec5SDimitry Andric
17190b57cec5SDimitry Andrictemplate<class _Fp, class _Alloc, class _Rp, class ..._ArgTypes>
17200b57cec5SDimitry Andricconst void*
17210b57cec5SDimitry Andric__func<_Fp, _Alloc, _Rp(_ArgTypes...)>::target(const type_info& __ti) const _NOEXCEPT
17220b57cec5SDimitry Andric{
17230b57cec5SDimitry Andric    if (__ti == typeid(_Fp))
17240b57cec5SDimitry Andric        return &__f_.__target();
17250b57cec5SDimitry Andric    return (const void*)0;
17260b57cec5SDimitry Andric}
17270b57cec5SDimitry Andric
17280b57cec5SDimitry Andrictemplate<class _Fp, class _Alloc, class _Rp, class ..._ArgTypes>
17290b57cec5SDimitry Andricconst std::type_info&
17300b57cec5SDimitry Andric__func<_Fp, _Alloc, _Rp(_ArgTypes...)>::target_type() const _NOEXCEPT
17310b57cec5SDimitry Andric{
17320b57cec5SDimitry Andric    return typeid(_Fp);
17330b57cec5SDimitry Andric}
17340b57cec5SDimitry Andric
17350b57cec5SDimitry Andric#endif  // _LIBCPP_NO_RTTI
17360b57cec5SDimitry Andric
17370b57cec5SDimitry Andric// __value_func creates a value-type from a __func.
17380b57cec5SDimitry Andric
17390b57cec5SDimitry Andrictemplate <class _Fp> class __value_func;
17400b57cec5SDimitry Andric
17410b57cec5SDimitry Andrictemplate <class _Rp, class... _ArgTypes> class __value_func<_Rp(_ArgTypes...)>
17420b57cec5SDimitry Andric{
17430b57cec5SDimitry Andric    typename aligned_storage<3 * sizeof(void*)>::type __buf_;
17440b57cec5SDimitry Andric
17450b57cec5SDimitry Andric    typedef __base<_Rp(_ArgTypes...)> __func;
17460b57cec5SDimitry Andric    __func* __f_;
17470b57cec5SDimitry Andric
17480b57cec5SDimitry Andric    _LIBCPP_NO_CFI static __func* __as_base(void* p)
17490b57cec5SDimitry Andric    {
17500b57cec5SDimitry Andric        return reinterpret_cast<__func*>(p);
17510b57cec5SDimitry Andric    }
17520b57cec5SDimitry Andric
17530b57cec5SDimitry Andric  public:
17540b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
17550b57cec5SDimitry Andric    __value_func() _NOEXCEPT : __f_(0) {}
17560b57cec5SDimitry Andric
17570b57cec5SDimitry Andric    template <class _Fp, class _Alloc>
17580b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY __value_func(_Fp&& __f, const _Alloc& __a)
17590b57cec5SDimitry Andric        : __f_(0)
17600b57cec5SDimitry Andric    {
17610b57cec5SDimitry Andric        typedef allocator_traits<_Alloc> __alloc_traits;
17620b57cec5SDimitry Andric        typedef __function::__func<_Fp, _Alloc, _Rp(_ArgTypes...)> _Fun;
17630b57cec5SDimitry Andric        typedef typename __rebind_alloc_helper<__alloc_traits, _Fun>::type
17640b57cec5SDimitry Andric            _FunAlloc;
17650b57cec5SDimitry Andric
17660b57cec5SDimitry Andric        if (__function::__not_null(__f))
17670b57cec5SDimitry Andric        {
17680b57cec5SDimitry Andric            _FunAlloc __af(__a);
17690b57cec5SDimitry Andric            if (sizeof(_Fun) <= sizeof(__buf_) &&
17700b57cec5SDimitry Andric                is_nothrow_copy_constructible<_Fp>::value &&
17710b57cec5SDimitry Andric                is_nothrow_copy_constructible<_FunAlloc>::value)
17720b57cec5SDimitry Andric            {
17730b57cec5SDimitry Andric                __f_ =
17740b57cec5SDimitry Andric                    ::new ((void*)&__buf_) _Fun(_VSTD::move(__f), _Alloc(__af));
17750b57cec5SDimitry Andric            }
17760b57cec5SDimitry Andric            else
17770b57cec5SDimitry Andric            {
17780b57cec5SDimitry Andric                typedef __allocator_destructor<_FunAlloc> _Dp;
17790b57cec5SDimitry Andric                unique_ptr<__func, _Dp> __hold(__af.allocate(1), _Dp(__af, 1));
17800b57cec5SDimitry Andric                ::new ((void*)__hold.get()) _Fun(_VSTD::move(__f), _Alloc(__a));
17810b57cec5SDimitry Andric                __f_ = __hold.release();
17820b57cec5SDimitry Andric            }
17830b57cec5SDimitry Andric        }
17840b57cec5SDimitry Andric    }
17850b57cec5SDimitry Andric
17860b57cec5SDimitry Andric    template <class _Fp,
17870b57cec5SDimitry Andric        class = typename enable_if<!is_same<typename decay<_Fp>::type, __value_func>::value>::type>
17880b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY explicit __value_func(_Fp&& __f)
17890b57cec5SDimitry Andric        : __value_func(std::forward<_Fp>(__f), allocator<_Fp>()) {}
17900b57cec5SDimitry Andric
17910b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
17920b57cec5SDimitry Andric    __value_func(const __value_func& __f)
17930b57cec5SDimitry Andric    {
17940b57cec5SDimitry Andric        if (__f.__f_ == 0)
17950b57cec5SDimitry Andric            __f_ = 0;
17960b57cec5SDimitry Andric        else if ((void*)__f.__f_ == &__f.__buf_)
17970b57cec5SDimitry Andric        {
17980b57cec5SDimitry Andric            __f_ = __as_base(&__buf_);
17990b57cec5SDimitry Andric            __f.__f_->__clone(__f_);
18000b57cec5SDimitry Andric        }
18010b57cec5SDimitry Andric        else
18020b57cec5SDimitry Andric            __f_ = __f.__f_->__clone();
18030b57cec5SDimitry Andric    }
18040b57cec5SDimitry Andric
18050b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
18060b57cec5SDimitry Andric    __value_func(__value_func&& __f) _NOEXCEPT
18070b57cec5SDimitry Andric    {
18080b57cec5SDimitry Andric        if (__f.__f_ == 0)
18090b57cec5SDimitry Andric            __f_ = 0;
18100b57cec5SDimitry Andric        else if ((void*)__f.__f_ == &__f.__buf_)
18110b57cec5SDimitry Andric        {
18120b57cec5SDimitry Andric            __f_ = __as_base(&__buf_);
18130b57cec5SDimitry Andric            __f.__f_->__clone(__f_);
18140b57cec5SDimitry Andric        }
18150b57cec5SDimitry Andric        else
18160b57cec5SDimitry Andric        {
18170b57cec5SDimitry Andric            __f_ = __f.__f_;
18180b57cec5SDimitry Andric            __f.__f_ = 0;
18190b57cec5SDimitry Andric        }
18200b57cec5SDimitry Andric    }
18210b57cec5SDimitry Andric
18220b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
18230b57cec5SDimitry Andric    ~__value_func()
18240b57cec5SDimitry Andric    {
18250b57cec5SDimitry Andric        if ((void*)__f_ == &__buf_)
18260b57cec5SDimitry Andric            __f_->destroy();
18270b57cec5SDimitry Andric        else if (__f_)
18280b57cec5SDimitry Andric            __f_->destroy_deallocate();
18290b57cec5SDimitry Andric    }
18300b57cec5SDimitry Andric
18310b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
18320b57cec5SDimitry Andric    __value_func& operator=(__value_func&& __f)
18330b57cec5SDimitry Andric    {
18340b57cec5SDimitry Andric        *this = nullptr;
18350b57cec5SDimitry Andric        if (__f.__f_ == 0)
18360b57cec5SDimitry Andric            __f_ = 0;
18370b57cec5SDimitry Andric        else if ((void*)__f.__f_ == &__f.__buf_)
18380b57cec5SDimitry Andric        {
18390b57cec5SDimitry Andric            __f_ = __as_base(&__buf_);
18400b57cec5SDimitry Andric            __f.__f_->__clone(__f_);
18410b57cec5SDimitry Andric        }
18420b57cec5SDimitry Andric        else
18430b57cec5SDimitry Andric        {
18440b57cec5SDimitry Andric            __f_ = __f.__f_;
18450b57cec5SDimitry Andric            __f.__f_ = 0;
18460b57cec5SDimitry Andric        }
18470b57cec5SDimitry Andric        return *this;
18480b57cec5SDimitry Andric    }
18490b57cec5SDimitry Andric
18500b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
18510b57cec5SDimitry Andric    __value_func& operator=(nullptr_t)
18520b57cec5SDimitry Andric    {
18530b57cec5SDimitry Andric        __func* __f = __f_;
18540b57cec5SDimitry Andric        __f_ = 0;
18550b57cec5SDimitry Andric        if ((void*)__f == &__buf_)
18560b57cec5SDimitry Andric            __f->destroy();
18570b57cec5SDimitry Andric        else if (__f)
18580b57cec5SDimitry Andric            __f->destroy_deallocate();
18590b57cec5SDimitry Andric        return *this;
18600b57cec5SDimitry Andric    }
18610b57cec5SDimitry Andric
18620b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
18630b57cec5SDimitry Andric    _Rp operator()(_ArgTypes&&... __args) const
18640b57cec5SDimitry Andric    {
18650b57cec5SDimitry Andric        if (__f_ == 0)
18660b57cec5SDimitry Andric            __throw_bad_function_call();
18670b57cec5SDimitry Andric        return (*__f_)(_VSTD::forward<_ArgTypes>(__args)...);
18680b57cec5SDimitry Andric    }
18690b57cec5SDimitry Andric
18700b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
18710b57cec5SDimitry Andric    void swap(__value_func& __f) _NOEXCEPT
18720b57cec5SDimitry Andric    {
18730b57cec5SDimitry Andric        if (&__f == this)
18740b57cec5SDimitry Andric            return;
18750b57cec5SDimitry Andric        if ((void*)__f_ == &__buf_ && (void*)__f.__f_ == &__f.__buf_)
18760b57cec5SDimitry Andric        {
18770b57cec5SDimitry Andric            typename aligned_storage<sizeof(__buf_)>::type __tempbuf;
18780b57cec5SDimitry Andric            __func* __t = __as_base(&__tempbuf);
18790b57cec5SDimitry Andric            __f_->__clone(__t);
18800b57cec5SDimitry Andric            __f_->destroy();
18810b57cec5SDimitry Andric            __f_ = 0;
18820b57cec5SDimitry Andric            __f.__f_->__clone(__as_base(&__buf_));
18830b57cec5SDimitry Andric            __f.__f_->destroy();
18840b57cec5SDimitry Andric            __f.__f_ = 0;
18850b57cec5SDimitry Andric            __f_ = __as_base(&__buf_);
18860b57cec5SDimitry Andric            __t->__clone(__as_base(&__f.__buf_));
18870b57cec5SDimitry Andric            __t->destroy();
18880b57cec5SDimitry Andric            __f.__f_ = __as_base(&__f.__buf_);
18890b57cec5SDimitry Andric        }
18900b57cec5SDimitry Andric        else if ((void*)__f_ == &__buf_)
18910b57cec5SDimitry Andric        {
18920b57cec5SDimitry Andric            __f_->__clone(__as_base(&__f.__buf_));
18930b57cec5SDimitry Andric            __f_->destroy();
18940b57cec5SDimitry Andric            __f_ = __f.__f_;
18950b57cec5SDimitry Andric            __f.__f_ = __as_base(&__f.__buf_);
18960b57cec5SDimitry Andric        }
18970b57cec5SDimitry Andric        else if ((void*)__f.__f_ == &__f.__buf_)
18980b57cec5SDimitry Andric        {
18990b57cec5SDimitry Andric            __f.__f_->__clone(__as_base(&__buf_));
19000b57cec5SDimitry Andric            __f.__f_->destroy();
19010b57cec5SDimitry Andric            __f.__f_ = __f_;
19020b57cec5SDimitry Andric            __f_ = __as_base(&__buf_);
19030b57cec5SDimitry Andric        }
19040b57cec5SDimitry Andric        else
19050b57cec5SDimitry Andric            _VSTD::swap(__f_, __f.__f_);
19060b57cec5SDimitry Andric    }
19070b57cec5SDimitry Andric
19080b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
19090b57cec5SDimitry Andric    _LIBCPP_EXPLICIT operator bool() const _NOEXCEPT { return __f_ != 0; }
19100b57cec5SDimitry Andric
19110b57cec5SDimitry Andric#ifndef _LIBCPP_NO_RTTI
19120b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
19130b57cec5SDimitry Andric    const std::type_info& target_type() const _NOEXCEPT
19140b57cec5SDimitry Andric    {
19150b57cec5SDimitry Andric        if (__f_ == 0)
19160b57cec5SDimitry Andric            return typeid(void);
19170b57cec5SDimitry Andric        return __f_->target_type();
19180b57cec5SDimitry Andric    }
19190b57cec5SDimitry Andric
19200b57cec5SDimitry Andric    template <typename _Tp>
19210b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY const _Tp* target() const _NOEXCEPT
19220b57cec5SDimitry Andric    {
19230b57cec5SDimitry Andric        if (__f_ == 0)
19240b57cec5SDimitry Andric            return 0;
19250b57cec5SDimitry Andric        return (const _Tp*)__f_->target(typeid(_Tp));
19260b57cec5SDimitry Andric    }
19270b57cec5SDimitry Andric#endif // _LIBCPP_NO_RTTI
19280b57cec5SDimitry Andric};
19290b57cec5SDimitry Andric
19300b57cec5SDimitry Andric// Storage for a functor object, to be used with __policy to manage copy and
19310b57cec5SDimitry Andric// destruction.
19320b57cec5SDimitry Andricunion __policy_storage
19330b57cec5SDimitry Andric{
19340b57cec5SDimitry Andric    mutable char __small[sizeof(void*) * 2];
19350b57cec5SDimitry Andric    void* __large;
19360b57cec5SDimitry Andric};
19370b57cec5SDimitry Andric
19380b57cec5SDimitry Andric// True if _Fun can safely be held in __policy_storage.__small.
19390b57cec5SDimitry Andrictemplate <typename _Fun>
19400b57cec5SDimitry Andricstruct __use_small_storage
19410b57cec5SDimitry Andric    : public _VSTD::integral_constant<
19420b57cec5SDimitry Andric          bool, sizeof(_Fun) <= sizeof(__policy_storage) &&
19430b57cec5SDimitry Andric                    _LIBCPP_ALIGNOF(_Fun) <= _LIBCPP_ALIGNOF(__policy_storage) &&
19440b57cec5SDimitry Andric                    _VSTD::is_trivially_copy_constructible<_Fun>::value &&
19450b57cec5SDimitry Andric                    _VSTD::is_trivially_destructible<_Fun>::value> {};
19460b57cec5SDimitry Andric
19470b57cec5SDimitry Andric// Policy contains information about how to copy, destroy, and move the
19480b57cec5SDimitry Andric// underlying functor. You can think of it as a vtable of sorts.
19490b57cec5SDimitry Andricstruct __policy
19500b57cec5SDimitry Andric{
19510b57cec5SDimitry Andric    // Used to copy or destroy __large values. null for trivial objects.
19520b57cec5SDimitry Andric    void* (*const __clone)(const void*);
19530b57cec5SDimitry Andric    void (*const __destroy)(void*);
19540b57cec5SDimitry Andric
19550b57cec5SDimitry Andric    // True if this is the null policy (no value).
19560b57cec5SDimitry Andric    const bool __is_null;
19570b57cec5SDimitry Andric
19580b57cec5SDimitry Andric    // The target type. May be null if RTTI is disabled.
19590b57cec5SDimitry Andric    const std::type_info* const __type_info;
19600b57cec5SDimitry Andric
19610b57cec5SDimitry Andric    // Returns a pointer to a static policy object suitable for the functor
19620b57cec5SDimitry Andric    // type.
19630b57cec5SDimitry Andric    template <typename _Fun>
19640b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY static const __policy* __create()
19650b57cec5SDimitry Andric    {
19660b57cec5SDimitry Andric        return __choose_policy<_Fun>(__use_small_storage<_Fun>());
19670b57cec5SDimitry Andric    }
19680b57cec5SDimitry Andric
19690b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
19700b57cec5SDimitry Andric    static const __policy* __create_empty()
19710b57cec5SDimitry Andric    {
19720b57cec5SDimitry Andric        static const _LIBCPP_CONSTEXPR __policy __policy_ = {nullptr, nullptr,
19730b57cec5SDimitry Andric                                                             true,
19740b57cec5SDimitry Andric#ifndef _LIBCPP_NO_RTTI
19750b57cec5SDimitry Andric                                                             &typeid(void)
19760b57cec5SDimitry Andric#else
19770b57cec5SDimitry Andric                                                             nullptr
19780b57cec5SDimitry Andric#endif
19790b57cec5SDimitry Andric        };
19800b57cec5SDimitry Andric        return &__policy_;
19810b57cec5SDimitry Andric    }
19820b57cec5SDimitry Andric
19830b57cec5SDimitry Andric  private:
19840b57cec5SDimitry Andric    template <typename _Fun> static void* __large_clone(const void* __s)
19850b57cec5SDimitry Andric    {
19860b57cec5SDimitry Andric        const _Fun* __f = static_cast<const _Fun*>(__s);
19870b57cec5SDimitry Andric        return __f->__clone();
19880b57cec5SDimitry Andric    }
19890b57cec5SDimitry Andric
19900b57cec5SDimitry Andric    template <typename _Fun>
19910b57cec5SDimitry Andric    static void __large_destroy(void* __s) {
19920b57cec5SDimitry Andric      _Fun::__destroy_and_delete(static_cast<_Fun*>(__s));
19930b57cec5SDimitry Andric    }
19940b57cec5SDimitry Andric
19950b57cec5SDimitry Andric    template <typename _Fun>
19960b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY static const __policy*
19970b57cec5SDimitry Andric    __choose_policy(/* is_small = */ false_type) {
19980b57cec5SDimitry Andric      static const _LIBCPP_CONSTEXPR __policy __policy_ = {
19990b57cec5SDimitry Andric          &__large_clone<_Fun>, &__large_destroy<_Fun>, false,
20000b57cec5SDimitry Andric#ifndef _LIBCPP_NO_RTTI
20010b57cec5SDimitry Andric          &typeid(typename _Fun::_Target)
20020b57cec5SDimitry Andric#else
20030b57cec5SDimitry Andric          nullptr
20040b57cec5SDimitry Andric#endif
20050b57cec5SDimitry Andric      };
20060b57cec5SDimitry Andric        return &__policy_;
20070b57cec5SDimitry Andric    }
20080b57cec5SDimitry Andric
20090b57cec5SDimitry Andric    template <typename _Fun>
20100b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY static const __policy*
20110b57cec5SDimitry Andric        __choose_policy(/* is_small = */ true_type)
20120b57cec5SDimitry Andric    {
20130b57cec5SDimitry Andric        static const _LIBCPP_CONSTEXPR __policy __policy_ = {
20140b57cec5SDimitry Andric            nullptr, nullptr, false,
20150b57cec5SDimitry Andric#ifndef _LIBCPP_NO_RTTI
20160b57cec5SDimitry Andric            &typeid(typename _Fun::_Target)
20170b57cec5SDimitry Andric#else
20180b57cec5SDimitry Andric            nullptr
20190b57cec5SDimitry Andric#endif
20200b57cec5SDimitry Andric        };
20210b57cec5SDimitry Andric        return &__policy_;
20220b57cec5SDimitry Andric    }
20230b57cec5SDimitry Andric};
20240b57cec5SDimitry Andric
20250b57cec5SDimitry Andric// Used to choose between perfect forwarding or pass-by-value. Pass-by-value is
20260b57cec5SDimitry Andric// faster for types that can be passed in registers.
20270b57cec5SDimitry Andrictemplate <typename _Tp>
20280b57cec5SDimitry Andricusing __fast_forward =
20290b57cec5SDimitry Andric    typename _VSTD::conditional<_VSTD::is_scalar<_Tp>::value, _Tp, _Tp&&>::type;
20300b57cec5SDimitry Andric
20310b57cec5SDimitry Andric// __policy_invoker calls an instance of __alloc_func held in __policy_storage.
20320b57cec5SDimitry Andric
20330b57cec5SDimitry Andrictemplate <class _Fp> struct __policy_invoker;
20340b57cec5SDimitry Andric
20350b57cec5SDimitry Andrictemplate <class _Rp, class... _ArgTypes>
20360b57cec5SDimitry Andricstruct __policy_invoker<_Rp(_ArgTypes...)>
20370b57cec5SDimitry Andric{
20380b57cec5SDimitry Andric    typedef _Rp (*__Call)(const __policy_storage*,
20390b57cec5SDimitry Andric                          __fast_forward<_ArgTypes>...);
20400b57cec5SDimitry Andric
20410b57cec5SDimitry Andric    __Call __call_;
20420b57cec5SDimitry Andric
20430b57cec5SDimitry Andric    // Creates an invoker that throws bad_function_call.
20440b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
20450b57cec5SDimitry Andric    __policy_invoker() : __call_(&__call_empty) {}
20460b57cec5SDimitry Andric
20470b57cec5SDimitry Andric    // Creates an invoker that calls the given instance of __func.
20480b57cec5SDimitry Andric    template <typename _Fun>
20490b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY static __policy_invoker __create()
20500b57cec5SDimitry Andric    {
20510b57cec5SDimitry Andric        return __policy_invoker(&__call_impl<_Fun>);
20520b57cec5SDimitry Andric    }
20530b57cec5SDimitry Andric
20540b57cec5SDimitry Andric  private:
20550b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
20560b57cec5SDimitry Andric    explicit __policy_invoker(__Call __c) : __call_(__c) {}
20570b57cec5SDimitry Andric
20580b57cec5SDimitry Andric    static _Rp __call_empty(const __policy_storage*,
20590b57cec5SDimitry Andric                            __fast_forward<_ArgTypes>...)
20600b57cec5SDimitry Andric    {
20610b57cec5SDimitry Andric        __throw_bad_function_call();
20620b57cec5SDimitry Andric    }
20630b57cec5SDimitry Andric
20640b57cec5SDimitry Andric    template <typename _Fun>
20650b57cec5SDimitry Andric    static _Rp __call_impl(const __policy_storage* __buf,
20660b57cec5SDimitry Andric                           __fast_forward<_ArgTypes>... __args)
20670b57cec5SDimitry Andric    {
20680b57cec5SDimitry Andric        _Fun* __f = reinterpret_cast<_Fun*>(__use_small_storage<_Fun>::value
20690b57cec5SDimitry Andric                                                ? &__buf->__small
20700b57cec5SDimitry Andric                                                : __buf->__large);
20710b57cec5SDimitry Andric        return (*__f)(_VSTD::forward<_ArgTypes>(__args)...);
20720b57cec5SDimitry Andric    }
20730b57cec5SDimitry Andric};
20740b57cec5SDimitry Andric
20750b57cec5SDimitry Andric// __policy_func uses a __policy and __policy_invoker to create a type-erased,
20760b57cec5SDimitry Andric// copyable functor.
20770b57cec5SDimitry Andric
20780b57cec5SDimitry Andrictemplate <class _Fp> class __policy_func;
20790b57cec5SDimitry Andric
20800b57cec5SDimitry Andrictemplate <class _Rp, class... _ArgTypes> class __policy_func<_Rp(_ArgTypes...)>
20810b57cec5SDimitry Andric{
20820b57cec5SDimitry Andric    // Inline storage for small objects.
20830b57cec5SDimitry Andric    __policy_storage __buf_;
20840b57cec5SDimitry Andric
20850b57cec5SDimitry Andric    // Calls the value stored in __buf_. This could technically be part of
20860b57cec5SDimitry Andric    // policy, but storing it here eliminates a level of indirection inside
20870b57cec5SDimitry Andric    // operator().
20880b57cec5SDimitry Andric    typedef __function::__policy_invoker<_Rp(_ArgTypes...)> __invoker;
20890b57cec5SDimitry Andric    __invoker __invoker_;
20900b57cec5SDimitry Andric
20910b57cec5SDimitry Andric    // The policy that describes how to move / copy / destroy __buf_. Never
20920b57cec5SDimitry Andric    // null, even if the function is empty.
20930b57cec5SDimitry Andric    const __policy* __policy_;
20940b57cec5SDimitry Andric
20950b57cec5SDimitry Andric  public:
20960b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
20970b57cec5SDimitry Andric    __policy_func() : __policy_(__policy::__create_empty()) {}
20980b57cec5SDimitry Andric
20990b57cec5SDimitry Andric    template <class _Fp, class _Alloc>
21000b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY __policy_func(_Fp&& __f, const _Alloc& __a)
21010b57cec5SDimitry Andric        : __policy_(__policy::__create_empty())
21020b57cec5SDimitry Andric    {
21030b57cec5SDimitry Andric        typedef __alloc_func<_Fp, _Alloc, _Rp(_ArgTypes...)> _Fun;
21040b57cec5SDimitry Andric        typedef allocator_traits<_Alloc> __alloc_traits;
21050b57cec5SDimitry Andric        typedef typename __rebind_alloc_helper<__alloc_traits, _Fun>::type
21060b57cec5SDimitry Andric            _FunAlloc;
21070b57cec5SDimitry Andric
21080b57cec5SDimitry Andric        if (__function::__not_null(__f))
21090b57cec5SDimitry Andric        {
21100b57cec5SDimitry Andric            __invoker_ = __invoker::template __create<_Fun>();
21110b57cec5SDimitry Andric            __policy_ = __policy::__create<_Fun>();
21120b57cec5SDimitry Andric
21130b57cec5SDimitry Andric            _FunAlloc __af(__a);
21140b57cec5SDimitry Andric            if (__use_small_storage<_Fun>())
21150b57cec5SDimitry Andric            {
21160b57cec5SDimitry Andric                ::new ((void*)&__buf_.__small)
21170b57cec5SDimitry Andric                    _Fun(_VSTD::move(__f), _Alloc(__af));
21180b57cec5SDimitry Andric            }
21190b57cec5SDimitry Andric            else
21200b57cec5SDimitry Andric            {
21210b57cec5SDimitry Andric                typedef __allocator_destructor<_FunAlloc> _Dp;
21220b57cec5SDimitry Andric                unique_ptr<_Fun, _Dp> __hold(__af.allocate(1), _Dp(__af, 1));
21230b57cec5SDimitry Andric                ::new ((void*)__hold.get())
21240b57cec5SDimitry Andric                    _Fun(_VSTD::move(__f), _Alloc(__af));
21250b57cec5SDimitry Andric                __buf_.__large = __hold.release();
21260b57cec5SDimitry Andric            }
21270b57cec5SDimitry Andric        }
21280b57cec5SDimitry Andric    }
21290b57cec5SDimitry Andric
21300b57cec5SDimitry Andric    template <class _Fp, class = typename enable_if<!is_same<typename decay<_Fp>::type, __policy_func>::value>::type>
21310b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY explicit __policy_func(_Fp&& __f)
21320b57cec5SDimitry Andric        : __policy_(__policy::__create_empty()) {
21330b57cec5SDimitry Andric      typedef __default_alloc_func<_Fp, _Rp(_ArgTypes...)> _Fun;
21340b57cec5SDimitry Andric
21350b57cec5SDimitry Andric      if (__function::__not_null(__f)) {
21360b57cec5SDimitry Andric        __invoker_ = __invoker::template __create<_Fun>();
21370b57cec5SDimitry Andric        __policy_ = __policy::__create<_Fun>();
21380b57cec5SDimitry Andric        if (__use_small_storage<_Fun>()) {
21390b57cec5SDimitry Andric          ::new ((void*)&__buf_.__small) _Fun(_VSTD::move(__f));
21400b57cec5SDimitry Andric        } else {
21410b57cec5SDimitry Andric          __builtin_new_allocator::__holder_t __hold =
21420b57cec5SDimitry Andric              __builtin_new_allocator::__allocate_type<_Fun>(1);
21430b57cec5SDimitry Andric          __buf_.__large = ::new (__hold.get()) _Fun(_VSTD::move(__f));
21440b57cec5SDimitry Andric          (void)__hold.release();
21450b57cec5SDimitry Andric        }
21460b57cec5SDimitry Andric      }
21470b57cec5SDimitry Andric    }
21480b57cec5SDimitry Andric
21490b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
21500b57cec5SDimitry Andric    __policy_func(const __policy_func& __f)
21510b57cec5SDimitry Andric        : __buf_(__f.__buf_), __invoker_(__f.__invoker_),
21520b57cec5SDimitry Andric          __policy_(__f.__policy_)
21530b57cec5SDimitry Andric    {
21540b57cec5SDimitry Andric        if (__policy_->__clone)
21550b57cec5SDimitry Andric            __buf_.__large = __policy_->__clone(__f.__buf_.__large);
21560b57cec5SDimitry Andric    }
21570b57cec5SDimitry Andric
21580b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
21590b57cec5SDimitry Andric    __policy_func(__policy_func&& __f)
21600b57cec5SDimitry Andric        : __buf_(__f.__buf_), __invoker_(__f.__invoker_),
21610b57cec5SDimitry Andric          __policy_(__f.__policy_)
21620b57cec5SDimitry Andric    {
21630b57cec5SDimitry Andric        if (__policy_->__destroy)
21640b57cec5SDimitry Andric        {
21650b57cec5SDimitry Andric            __f.__policy_ = __policy::__create_empty();
21660b57cec5SDimitry Andric            __f.__invoker_ = __invoker();
21670b57cec5SDimitry Andric        }
21680b57cec5SDimitry Andric    }
21690b57cec5SDimitry Andric
21700b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
21710b57cec5SDimitry Andric    ~__policy_func()
21720b57cec5SDimitry Andric    {
21730b57cec5SDimitry Andric        if (__policy_->__destroy)
21740b57cec5SDimitry Andric            __policy_->__destroy(__buf_.__large);
21750b57cec5SDimitry Andric    }
21760b57cec5SDimitry Andric
21770b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
21780b57cec5SDimitry Andric    __policy_func& operator=(__policy_func&& __f)
21790b57cec5SDimitry Andric    {
21800b57cec5SDimitry Andric        *this = nullptr;
21810b57cec5SDimitry Andric        __buf_ = __f.__buf_;
21820b57cec5SDimitry Andric        __invoker_ = __f.__invoker_;
21830b57cec5SDimitry Andric        __policy_ = __f.__policy_;
21840b57cec5SDimitry Andric        __f.__policy_ = __policy::__create_empty();
21850b57cec5SDimitry Andric        __f.__invoker_ = __invoker();
21860b57cec5SDimitry Andric        return *this;
21870b57cec5SDimitry Andric    }
21880b57cec5SDimitry Andric
21890b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
21900b57cec5SDimitry Andric    __policy_func& operator=(nullptr_t)
21910b57cec5SDimitry Andric    {
21920b57cec5SDimitry Andric        const __policy* __p = __policy_;
21930b57cec5SDimitry Andric        __policy_ = __policy::__create_empty();
21940b57cec5SDimitry Andric        __invoker_ = __invoker();
21950b57cec5SDimitry Andric        if (__p->__destroy)
21960b57cec5SDimitry Andric            __p->__destroy(__buf_.__large);
21970b57cec5SDimitry Andric        return *this;
21980b57cec5SDimitry Andric    }
21990b57cec5SDimitry Andric
22000b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
22010b57cec5SDimitry Andric    _Rp operator()(_ArgTypes&&... __args) const
22020b57cec5SDimitry Andric    {
22030b57cec5SDimitry Andric        return __invoker_.__call_(_VSTD::addressof(__buf_),
22040b57cec5SDimitry Andric                                  _VSTD::forward<_ArgTypes>(__args)...);
22050b57cec5SDimitry Andric    }
22060b57cec5SDimitry Andric
22070b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
22080b57cec5SDimitry Andric    void swap(__policy_func& __f)
22090b57cec5SDimitry Andric    {
22100b57cec5SDimitry Andric        _VSTD::swap(__invoker_, __f.__invoker_);
22110b57cec5SDimitry Andric        _VSTD::swap(__policy_, __f.__policy_);
22120b57cec5SDimitry Andric        _VSTD::swap(__buf_, __f.__buf_);
22130b57cec5SDimitry Andric    }
22140b57cec5SDimitry Andric
22150b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
22160b57cec5SDimitry Andric    explicit operator bool() const _NOEXCEPT
22170b57cec5SDimitry Andric    {
22180b57cec5SDimitry Andric        return !__policy_->__is_null;
22190b57cec5SDimitry Andric    }
22200b57cec5SDimitry Andric
22210b57cec5SDimitry Andric#ifndef _LIBCPP_NO_RTTI
22220b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
22230b57cec5SDimitry Andric    const std::type_info& target_type() const _NOEXCEPT
22240b57cec5SDimitry Andric    {
22250b57cec5SDimitry Andric        return *__policy_->__type_info;
22260b57cec5SDimitry Andric    }
22270b57cec5SDimitry Andric
22280b57cec5SDimitry Andric    template <typename _Tp>
22290b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY const _Tp* target() const _NOEXCEPT
22300b57cec5SDimitry Andric    {
22310b57cec5SDimitry Andric        if (__policy_->__is_null || typeid(_Tp) != *__policy_->__type_info)
22320b57cec5SDimitry Andric            return nullptr;
22330b57cec5SDimitry Andric        if (__policy_->__clone) // Out of line storage.
22340b57cec5SDimitry Andric            return reinterpret_cast<const _Tp*>(__buf_.__large);
22350b57cec5SDimitry Andric        else
22360b57cec5SDimitry Andric            return reinterpret_cast<const _Tp*>(&__buf_.__small);
22370b57cec5SDimitry Andric    }
22380b57cec5SDimitry Andric#endif // _LIBCPP_NO_RTTI
22390b57cec5SDimitry Andric};
22400b57cec5SDimitry Andric
22410b57cec5SDimitry Andric}  // __function
22420b57cec5SDimitry Andric
22430b57cec5SDimitry Andrictemplate<class _Rp, class ..._ArgTypes>
22440b57cec5SDimitry Andricclass _LIBCPP_TEMPLATE_VIS function<_Rp(_ArgTypes...)>
22450b57cec5SDimitry Andric    : public __function::__maybe_derive_from_unary_function<_Rp(_ArgTypes...)>,
22460b57cec5SDimitry Andric      public __function::__maybe_derive_from_binary_function<_Rp(_ArgTypes...)>
22470b57cec5SDimitry Andric{
22480b57cec5SDimitry Andric#ifndef _LIBCPP_ABI_OPTIMIZED_FUNCTION
22490b57cec5SDimitry Andric    typedef __function::__value_func<_Rp(_ArgTypes...)> __func;
22500b57cec5SDimitry Andric#else
22510b57cec5SDimitry Andric    typedef __function::__policy_func<_Rp(_ArgTypes...)> __func;
22520b57cec5SDimitry Andric#endif
22530b57cec5SDimitry Andric
22540b57cec5SDimitry Andric    __func __f_;
22550b57cec5SDimitry Andric
22560b57cec5SDimitry Andric    template <class _Fp, bool = _And<
22570b57cec5SDimitry Andric        _IsNotSame<__uncvref_t<_Fp>, function>,
22580b57cec5SDimitry Andric        __invokable<_Fp&, _ArgTypes...>
22590b57cec5SDimitry Andric    >::value>
22600b57cec5SDimitry Andric    struct __callable;
22610b57cec5SDimitry Andric    template <class _Fp>
22620b57cec5SDimitry Andric        struct __callable<_Fp, true>
22630b57cec5SDimitry Andric        {
22640b57cec5SDimitry Andric            static const bool value = is_same<void, _Rp>::value ||
22650b57cec5SDimitry Andric                is_convertible<typename __invoke_of<_Fp&, _ArgTypes...>::type,
22660b57cec5SDimitry Andric                               _Rp>::value;
22670b57cec5SDimitry Andric        };
22680b57cec5SDimitry Andric    template <class _Fp>
22690b57cec5SDimitry Andric        struct __callable<_Fp, false>
22700b57cec5SDimitry Andric        {
22710b57cec5SDimitry Andric            static const bool value = false;
22720b57cec5SDimitry Andric        };
22730b57cec5SDimitry Andric
22740b57cec5SDimitry Andric  template <class _Fp>
22750b57cec5SDimitry Andric  using _EnableIfCallable = typename enable_if<__callable<_Fp>::value>::type;
22760b57cec5SDimitry Andricpublic:
22770b57cec5SDimitry Andric    typedef _Rp result_type;
22780b57cec5SDimitry Andric
22790b57cec5SDimitry Andric    // construct/copy/destroy:
22800b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
22810b57cec5SDimitry Andric    function() _NOEXCEPT { }
22820b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
22830b57cec5SDimitry Andric    function(nullptr_t) _NOEXCEPT {}
22840b57cec5SDimitry Andric    function(const function&);
22850b57cec5SDimitry Andric    function(function&&) _NOEXCEPT;
22860b57cec5SDimitry Andric    template<class _Fp, class = _EnableIfCallable<_Fp>>
22870b57cec5SDimitry Andric    function(_Fp);
22880b57cec5SDimitry Andric
22890b57cec5SDimitry Andric#if _LIBCPP_STD_VER <= 14
22900b57cec5SDimitry Andric    template<class _Alloc>
22910b57cec5SDimitry Andric      _LIBCPP_INLINE_VISIBILITY
22920b57cec5SDimitry Andric      function(allocator_arg_t, const _Alloc&) _NOEXCEPT {}
22930b57cec5SDimitry Andric    template<class _Alloc>
22940b57cec5SDimitry Andric      _LIBCPP_INLINE_VISIBILITY
22950b57cec5SDimitry Andric      function(allocator_arg_t, const _Alloc&, nullptr_t) _NOEXCEPT {}
22960b57cec5SDimitry Andric    template<class _Alloc>
22970b57cec5SDimitry Andric      function(allocator_arg_t, const _Alloc&, const function&);
22980b57cec5SDimitry Andric    template<class _Alloc>
22990b57cec5SDimitry Andric      function(allocator_arg_t, const _Alloc&, function&&);
23000b57cec5SDimitry Andric    template<class _Fp, class _Alloc, class = _EnableIfCallable<_Fp>>
23010b57cec5SDimitry Andric      function(allocator_arg_t, const _Alloc& __a, _Fp __f);
23020b57cec5SDimitry Andric#endif
23030b57cec5SDimitry Andric
23040b57cec5SDimitry Andric    function& operator=(const function&);
23050b57cec5SDimitry Andric    function& operator=(function&&) _NOEXCEPT;
23060b57cec5SDimitry Andric    function& operator=(nullptr_t) _NOEXCEPT;
23070b57cec5SDimitry Andric    template<class _Fp, class = _EnableIfCallable<_Fp>>
23080b57cec5SDimitry Andric    function& operator=(_Fp&&);
23090b57cec5SDimitry Andric
23100b57cec5SDimitry Andric    ~function();
23110b57cec5SDimitry Andric
23120b57cec5SDimitry Andric    // function modifiers:
23130b57cec5SDimitry Andric    void swap(function&) _NOEXCEPT;
23140b57cec5SDimitry Andric
23150b57cec5SDimitry Andric#if _LIBCPP_STD_VER <= 14
23160b57cec5SDimitry Andric    template<class _Fp, class _Alloc>
23170b57cec5SDimitry Andric      _LIBCPP_INLINE_VISIBILITY
23180b57cec5SDimitry Andric      void assign(_Fp&& __f, const _Alloc& __a)
23190b57cec5SDimitry Andric        {function(allocator_arg, __a, _VSTD::forward<_Fp>(__f)).swap(*this);}
23200b57cec5SDimitry Andric#endif
23210b57cec5SDimitry Andric
23220b57cec5SDimitry Andric    // function capacity:
23230b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
23240b57cec5SDimitry Andric    _LIBCPP_EXPLICIT operator bool() const _NOEXCEPT {
23250b57cec5SDimitry Andric      return static_cast<bool>(__f_);
23260b57cec5SDimitry Andric    }
23270b57cec5SDimitry Andric
23280b57cec5SDimitry Andric    // deleted overloads close possible hole in the type system
23290b57cec5SDimitry Andric    template<class _R2, class... _ArgTypes2>
23300b57cec5SDimitry Andric      bool operator==(const function<_R2(_ArgTypes2...)>&) const = delete;
23310b57cec5SDimitry Andric    template<class _R2, class... _ArgTypes2>
23320b57cec5SDimitry Andric      bool operator!=(const function<_R2(_ArgTypes2...)>&) const = delete;
23330b57cec5SDimitry Andricpublic:
23340b57cec5SDimitry Andric    // function invocation:
23350b57cec5SDimitry Andric    _Rp operator()(_ArgTypes...) const;
23360b57cec5SDimitry Andric
23370b57cec5SDimitry Andric#ifndef _LIBCPP_NO_RTTI
23380b57cec5SDimitry Andric    // function target access:
23390b57cec5SDimitry Andric    const std::type_info& target_type() const _NOEXCEPT;
23400b57cec5SDimitry Andric    template <typename _Tp> _Tp* target() _NOEXCEPT;
23410b57cec5SDimitry Andric    template <typename _Tp> const _Tp* target() const _NOEXCEPT;
23420b57cec5SDimitry Andric#endif  // _LIBCPP_NO_RTTI
23430b57cec5SDimitry Andric};
23440b57cec5SDimitry Andric
2345*e40139ffSDimitry Andric#ifndef _LIBCPP_HAS_NO_DEDUCTION_GUIDES
2346*e40139ffSDimitry Andrictemplate<class _Rp, class ..._Ap>
2347*e40139ffSDimitry Andricfunction(_Rp(*)(_Ap...)) -> function<_Rp(_Ap...)>;
2348*e40139ffSDimitry Andric
2349*e40139ffSDimitry Andrictemplate<class _Fp>
2350*e40139ffSDimitry Andricstruct __strip_signature;
2351*e40139ffSDimitry Andric
2352*e40139ffSDimitry Andrictemplate<class _Rp, class _Gp, class ..._Ap>
2353*e40139ffSDimitry Andricstruct __strip_signature<_Rp (_Gp::*) (_Ap...)> { using type = _Rp(_Ap...); };
2354*e40139ffSDimitry Andrictemplate<class _Rp, class _Gp, class ..._Ap>
2355*e40139ffSDimitry Andricstruct __strip_signature<_Rp (_Gp::*) (_Ap...) const> { using type = _Rp(_Ap...); };
2356*e40139ffSDimitry Andrictemplate<class _Rp, class _Gp, class ..._Ap>
2357*e40139ffSDimitry Andricstruct __strip_signature<_Rp (_Gp::*) (_Ap...) volatile> { using type = _Rp(_Ap...); };
2358*e40139ffSDimitry Andrictemplate<class _Rp, class _Gp, class ..._Ap>
2359*e40139ffSDimitry Andricstruct __strip_signature<_Rp (_Gp::*) (_Ap...) const volatile> { using type = _Rp(_Ap...); };
2360*e40139ffSDimitry Andric
2361*e40139ffSDimitry Andrictemplate<class _Rp, class _Gp, class ..._Ap>
2362*e40139ffSDimitry Andricstruct __strip_signature<_Rp (_Gp::*) (_Ap...) &> { using type = _Rp(_Ap...); };
2363*e40139ffSDimitry Andrictemplate<class _Rp, class _Gp, class ..._Ap>
2364*e40139ffSDimitry Andricstruct __strip_signature<_Rp (_Gp::*) (_Ap...) const &> { using type = _Rp(_Ap...); };
2365*e40139ffSDimitry Andrictemplate<class _Rp, class _Gp, class ..._Ap>
2366*e40139ffSDimitry Andricstruct __strip_signature<_Rp (_Gp::*) (_Ap...) volatile &> { using type = _Rp(_Ap...); };
2367*e40139ffSDimitry Andrictemplate<class _Rp, class _Gp, class ..._Ap>
2368*e40139ffSDimitry Andricstruct __strip_signature<_Rp (_Gp::*) (_Ap...) const volatile &> { using type = _Rp(_Ap...); };
2369*e40139ffSDimitry Andric
2370*e40139ffSDimitry Andrictemplate<class _Rp, class _Gp, class ..._Ap>
2371*e40139ffSDimitry Andricstruct __strip_signature<_Rp (_Gp::*) (_Ap...) noexcept> { using type = _Rp(_Ap...); };
2372*e40139ffSDimitry Andrictemplate<class _Rp, class _Gp, class ..._Ap>
2373*e40139ffSDimitry Andricstruct __strip_signature<_Rp (_Gp::*) (_Ap...) const noexcept> { using type = _Rp(_Ap...); };
2374*e40139ffSDimitry Andrictemplate<class _Rp, class _Gp, class ..._Ap>
2375*e40139ffSDimitry Andricstruct __strip_signature<_Rp (_Gp::*) (_Ap...) volatile noexcept> { using type = _Rp(_Ap...); };
2376*e40139ffSDimitry Andrictemplate<class _Rp, class _Gp, class ..._Ap>
2377*e40139ffSDimitry Andricstruct __strip_signature<_Rp (_Gp::*) (_Ap...) const volatile noexcept> { using type = _Rp(_Ap...); };
2378*e40139ffSDimitry Andric
2379*e40139ffSDimitry Andrictemplate<class _Rp, class _Gp, class ..._Ap>
2380*e40139ffSDimitry Andricstruct __strip_signature<_Rp (_Gp::*) (_Ap...) & noexcept> { using type = _Rp(_Ap...); };
2381*e40139ffSDimitry Andrictemplate<class _Rp, class _Gp, class ..._Ap>
2382*e40139ffSDimitry Andricstruct __strip_signature<_Rp (_Gp::*) (_Ap...) const & noexcept> { using type = _Rp(_Ap...); };
2383*e40139ffSDimitry Andrictemplate<class _Rp, class _Gp, class ..._Ap>
2384*e40139ffSDimitry Andricstruct __strip_signature<_Rp (_Gp::*) (_Ap...) volatile & noexcept> { using type = _Rp(_Ap...); };
2385*e40139ffSDimitry Andrictemplate<class _Rp, class _Gp, class ..._Ap>
2386*e40139ffSDimitry Andricstruct __strip_signature<_Rp (_Gp::*) (_Ap...) const volatile & noexcept> { using type = _Rp(_Ap...); };
2387*e40139ffSDimitry Andric
2388*e40139ffSDimitry Andrictemplate<class _Fp, class _Stripped = typename __strip_signature<decltype(&_Fp::operator())>::type>
2389*e40139ffSDimitry Andricfunction(_Fp) -> function<_Stripped>;
2390*e40139ffSDimitry Andric#endif // !_LIBCPP_HAS_NO_DEDUCTION_GUIDES
2391*e40139ffSDimitry Andric
23920b57cec5SDimitry Andrictemplate<class _Rp, class ..._ArgTypes>
23930b57cec5SDimitry Andricfunction<_Rp(_ArgTypes...)>::function(const function& __f) : __f_(__f.__f_) {}
23940b57cec5SDimitry Andric
23950b57cec5SDimitry Andric#if _LIBCPP_STD_VER <= 14
23960b57cec5SDimitry Andrictemplate<class _Rp, class ..._ArgTypes>
23970b57cec5SDimitry Andrictemplate <class _Alloc>
23980b57cec5SDimitry Andricfunction<_Rp(_ArgTypes...)>::function(allocator_arg_t, const _Alloc&,
23990b57cec5SDimitry Andric                                     const function& __f) : __f_(__f.__f_) {}
24000b57cec5SDimitry Andric#endif
24010b57cec5SDimitry Andric
24020b57cec5SDimitry Andrictemplate <class _Rp, class... _ArgTypes>
24030b57cec5SDimitry Andricfunction<_Rp(_ArgTypes...)>::function(function&& __f) _NOEXCEPT
24040b57cec5SDimitry Andric    : __f_(_VSTD::move(__f.__f_)) {}
24050b57cec5SDimitry Andric
24060b57cec5SDimitry Andric#if _LIBCPP_STD_VER <= 14
24070b57cec5SDimitry Andrictemplate<class _Rp, class ..._ArgTypes>
24080b57cec5SDimitry Andrictemplate <class _Alloc>
24090b57cec5SDimitry Andricfunction<_Rp(_ArgTypes...)>::function(allocator_arg_t, const _Alloc&,
24100b57cec5SDimitry Andric                                      function&& __f)
24110b57cec5SDimitry Andric    : __f_(_VSTD::move(__f.__f_)) {}
24120b57cec5SDimitry Andric#endif
24130b57cec5SDimitry Andric
24140b57cec5SDimitry Andrictemplate <class _Rp, class... _ArgTypes>
24150b57cec5SDimitry Andrictemplate <class _Fp, class>
24160b57cec5SDimitry Andricfunction<_Rp(_ArgTypes...)>::function(_Fp __f) : __f_(_VSTD::move(__f)) {}
24170b57cec5SDimitry Andric
24180b57cec5SDimitry Andric#if _LIBCPP_STD_VER <= 14
24190b57cec5SDimitry Andrictemplate <class _Rp, class... _ArgTypes>
24200b57cec5SDimitry Andrictemplate <class _Fp, class _Alloc, class>
24210b57cec5SDimitry Andricfunction<_Rp(_ArgTypes...)>::function(allocator_arg_t, const _Alloc& __a,
24220b57cec5SDimitry Andric                                      _Fp __f)
24230b57cec5SDimitry Andric    : __f_(_VSTD::move(__f), __a) {}
24240b57cec5SDimitry Andric#endif
24250b57cec5SDimitry Andric
24260b57cec5SDimitry Andrictemplate<class _Rp, class ..._ArgTypes>
24270b57cec5SDimitry Andricfunction<_Rp(_ArgTypes...)>&
24280b57cec5SDimitry Andricfunction<_Rp(_ArgTypes...)>::operator=(const function& __f)
24290b57cec5SDimitry Andric{
24300b57cec5SDimitry Andric    function(__f).swap(*this);
24310b57cec5SDimitry Andric    return *this;
24320b57cec5SDimitry Andric}
24330b57cec5SDimitry Andric
24340b57cec5SDimitry Andrictemplate<class _Rp, class ..._ArgTypes>
24350b57cec5SDimitry Andricfunction<_Rp(_ArgTypes...)>&
24360b57cec5SDimitry Andricfunction<_Rp(_ArgTypes...)>::operator=(function&& __f) _NOEXCEPT
24370b57cec5SDimitry Andric{
24380b57cec5SDimitry Andric    __f_ = std::move(__f.__f_);
24390b57cec5SDimitry Andric    return *this;
24400b57cec5SDimitry Andric}
24410b57cec5SDimitry Andric
24420b57cec5SDimitry Andrictemplate<class _Rp, class ..._ArgTypes>
24430b57cec5SDimitry Andricfunction<_Rp(_ArgTypes...)>&
24440b57cec5SDimitry Andricfunction<_Rp(_ArgTypes...)>::operator=(nullptr_t) _NOEXCEPT
24450b57cec5SDimitry Andric{
24460b57cec5SDimitry Andric    __f_ = nullptr;
24470b57cec5SDimitry Andric    return *this;
24480b57cec5SDimitry Andric}
24490b57cec5SDimitry Andric
24500b57cec5SDimitry Andrictemplate<class _Rp, class ..._ArgTypes>
24510b57cec5SDimitry Andrictemplate <class _Fp, class>
24520b57cec5SDimitry Andricfunction<_Rp(_ArgTypes...)>&
24530b57cec5SDimitry Andricfunction<_Rp(_ArgTypes...)>::operator=(_Fp&& __f)
24540b57cec5SDimitry Andric{
24550b57cec5SDimitry Andric    function(_VSTD::forward<_Fp>(__f)).swap(*this);
24560b57cec5SDimitry Andric    return *this;
24570b57cec5SDimitry Andric}
24580b57cec5SDimitry Andric
24590b57cec5SDimitry Andrictemplate<class _Rp, class ..._ArgTypes>
24600b57cec5SDimitry Andricfunction<_Rp(_ArgTypes...)>::~function() {}
24610b57cec5SDimitry Andric
24620b57cec5SDimitry Andrictemplate<class _Rp, class ..._ArgTypes>
24630b57cec5SDimitry Andricvoid
24640b57cec5SDimitry Andricfunction<_Rp(_ArgTypes...)>::swap(function& __f) _NOEXCEPT
24650b57cec5SDimitry Andric{
24660b57cec5SDimitry Andric    __f_.swap(__f.__f_);
24670b57cec5SDimitry Andric}
24680b57cec5SDimitry Andric
24690b57cec5SDimitry Andrictemplate<class _Rp, class ..._ArgTypes>
24700b57cec5SDimitry Andric_Rp
24710b57cec5SDimitry Andricfunction<_Rp(_ArgTypes...)>::operator()(_ArgTypes... __arg) const
24720b57cec5SDimitry Andric{
24730b57cec5SDimitry Andric    return __f_(_VSTD::forward<_ArgTypes>(__arg)...);
24740b57cec5SDimitry Andric}
24750b57cec5SDimitry Andric
24760b57cec5SDimitry Andric#ifndef _LIBCPP_NO_RTTI
24770b57cec5SDimitry Andric
24780b57cec5SDimitry Andrictemplate<class _Rp, class ..._ArgTypes>
24790b57cec5SDimitry Andricconst std::type_info&
24800b57cec5SDimitry Andricfunction<_Rp(_ArgTypes...)>::target_type() const _NOEXCEPT
24810b57cec5SDimitry Andric{
24820b57cec5SDimitry Andric    return __f_.target_type();
24830b57cec5SDimitry Andric}
24840b57cec5SDimitry Andric
24850b57cec5SDimitry Andrictemplate<class _Rp, class ..._ArgTypes>
24860b57cec5SDimitry Andrictemplate <typename _Tp>
24870b57cec5SDimitry Andric_Tp*
24880b57cec5SDimitry Andricfunction<_Rp(_ArgTypes...)>::target() _NOEXCEPT
24890b57cec5SDimitry Andric{
24900b57cec5SDimitry Andric    return (_Tp*)(__f_.template target<_Tp>());
24910b57cec5SDimitry Andric}
24920b57cec5SDimitry Andric
24930b57cec5SDimitry Andrictemplate<class _Rp, class ..._ArgTypes>
24940b57cec5SDimitry Andrictemplate <typename _Tp>
24950b57cec5SDimitry Andricconst _Tp*
24960b57cec5SDimitry Andricfunction<_Rp(_ArgTypes...)>::target() const _NOEXCEPT
24970b57cec5SDimitry Andric{
24980b57cec5SDimitry Andric    return __f_.template target<_Tp>();
24990b57cec5SDimitry Andric}
25000b57cec5SDimitry Andric
25010b57cec5SDimitry Andric#endif  // _LIBCPP_NO_RTTI
25020b57cec5SDimitry Andric
25030b57cec5SDimitry Andrictemplate <class _Rp, class... _ArgTypes>
25040b57cec5SDimitry Andricinline _LIBCPP_INLINE_VISIBILITY
25050b57cec5SDimitry Andricbool
25060b57cec5SDimitry Andricoperator==(const function<_Rp(_ArgTypes...)>& __f, nullptr_t) _NOEXCEPT {return !__f;}
25070b57cec5SDimitry Andric
25080b57cec5SDimitry Andrictemplate <class _Rp, class... _ArgTypes>
25090b57cec5SDimitry Andricinline _LIBCPP_INLINE_VISIBILITY
25100b57cec5SDimitry Andricbool
25110b57cec5SDimitry Andricoperator==(nullptr_t, const function<_Rp(_ArgTypes...)>& __f) _NOEXCEPT {return !__f;}
25120b57cec5SDimitry Andric
25130b57cec5SDimitry Andrictemplate <class _Rp, class... _ArgTypes>
25140b57cec5SDimitry Andricinline _LIBCPP_INLINE_VISIBILITY
25150b57cec5SDimitry Andricbool
25160b57cec5SDimitry Andricoperator!=(const function<_Rp(_ArgTypes...)>& __f, nullptr_t) _NOEXCEPT {return (bool)__f;}
25170b57cec5SDimitry Andric
25180b57cec5SDimitry Andrictemplate <class _Rp, class... _ArgTypes>
25190b57cec5SDimitry Andricinline _LIBCPP_INLINE_VISIBILITY
25200b57cec5SDimitry Andricbool
25210b57cec5SDimitry Andricoperator!=(nullptr_t, const function<_Rp(_ArgTypes...)>& __f) _NOEXCEPT {return (bool)__f;}
25220b57cec5SDimitry Andric
25230b57cec5SDimitry Andrictemplate <class _Rp, class... _ArgTypes>
25240b57cec5SDimitry Andricinline _LIBCPP_INLINE_VISIBILITY
25250b57cec5SDimitry Andricvoid
25260b57cec5SDimitry Andricswap(function<_Rp(_ArgTypes...)>& __x, function<_Rp(_ArgTypes...)>& __y) _NOEXCEPT
25270b57cec5SDimitry Andric{return __x.swap(__y);}
25280b57cec5SDimitry Andric
25290b57cec5SDimitry Andric#else // _LIBCPP_CXX03_LANG
25300b57cec5SDimitry Andric
25310b57cec5SDimitry Andric#include <__functional_03>
25320b57cec5SDimitry Andric
25330b57cec5SDimitry Andric#endif
25340b57cec5SDimitry Andric
25350b57cec5SDimitry Andric////////////////////////////////////////////////////////////////////////////////
25360b57cec5SDimitry Andric//                                  BIND
25370b57cec5SDimitry Andric//==============================================================================
25380b57cec5SDimitry Andric
25390b57cec5SDimitry Andrictemplate<class _Tp> struct __is_bind_expression : public false_type {};
25400b57cec5SDimitry Andrictemplate<class _Tp> struct _LIBCPP_TEMPLATE_VIS is_bind_expression
25410b57cec5SDimitry Andric    : public __is_bind_expression<typename remove_cv<_Tp>::type> {};
25420b57cec5SDimitry Andric
25430b57cec5SDimitry Andric#if _LIBCPP_STD_VER > 14
25440b57cec5SDimitry Andrictemplate <class _Tp>
25450b57cec5SDimitry Andric_LIBCPP_INLINE_VAR constexpr size_t is_bind_expression_v = is_bind_expression<_Tp>::value;
25460b57cec5SDimitry Andric#endif
25470b57cec5SDimitry Andric
25480b57cec5SDimitry Andrictemplate<class _Tp> struct __is_placeholder : public integral_constant<int, 0> {};
25490b57cec5SDimitry Andrictemplate<class _Tp> struct _LIBCPP_TEMPLATE_VIS is_placeholder
25500b57cec5SDimitry Andric    : public __is_placeholder<typename remove_cv<_Tp>::type> {};
25510b57cec5SDimitry Andric
25520b57cec5SDimitry Andric#if _LIBCPP_STD_VER > 14
25530b57cec5SDimitry Andrictemplate <class _Tp>
25540b57cec5SDimitry Andric_LIBCPP_INLINE_VAR constexpr size_t is_placeholder_v = is_placeholder<_Tp>::value;
25550b57cec5SDimitry Andric#endif
25560b57cec5SDimitry Andric
25570b57cec5SDimitry Andricnamespace placeholders
25580b57cec5SDimitry Andric{
25590b57cec5SDimitry Andric
25600b57cec5SDimitry Andrictemplate <int _Np> struct __ph {};
25610b57cec5SDimitry Andric
25620b57cec5SDimitry Andric#if defined(_LIBCPP_CXX03_LANG) || defined(_LIBCPP_BUILDING_LIBRARY)
25630b57cec5SDimitry Andric_LIBCPP_FUNC_VIS extern const __ph<1>   _1;
25640b57cec5SDimitry Andric_LIBCPP_FUNC_VIS extern const __ph<2>   _2;
25650b57cec5SDimitry Andric_LIBCPP_FUNC_VIS extern const __ph<3>   _3;
25660b57cec5SDimitry Andric_LIBCPP_FUNC_VIS extern const __ph<4>   _4;
25670b57cec5SDimitry Andric_LIBCPP_FUNC_VIS extern const __ph<5>   _5;
25680b57cec5SDimitry Andric_LIBCPP_FUNC_VIS extern const __ph<6>   _6;
25690b57cec5SDimitry Andric_LIBCPP_FUNC_VIS extern const __ph<7>   _7;
25700b57cec5SDimitry Andric_LIBCPP_FUNC_VIS extern const __ph<8>   _8;
25710b57cec5SDimitry Andric_LIBCPP_FUNC_VIS extern const __ph<9>   _9;
25720b57cec5SDimitry Andric_LIBCPP_FUNC_VIS extern const __ph<10> _10;
25730b57cec5SDimitry Andric#else
25740b57cec5SDimitry Andric/* _LIBCPP_INLINE_VAR */ constexpr __ph<1>   _1{};
25750b57cec5SDimitry Andric/* _LIBCPP_INLINE_VAR */ constexpr __ph<2>   _2{};
25760b57cec5SDimitry Andric/* _LIBCPP_INLINE_VAR */ constexpr __ph<3>   _3{};
25770b57cec5SDimitry Andric/* _LIBCPP_INLINE_VAR */ constexpr __ph<4>   _4{};
25780b57cec5SDimitry Andric/* _LIBCPP_INLINE_VAR */ constexpr __ph<5>   _5{};
25790b57cec5SDimitry Andric/* _LIBCPP_INLINE_VAR */ constexpr __ph<6>   _6{};
25800b57cec5SDimitry Andric/* _LIBCPP_INLINE_VAR */ constexpr __ph<7>   _7{};
25810b57cec5SDimitry Andric/* _LIBCPP_INLINE_VAR */ constexpr __ph<8>   _8{};
25820b57cec5SDimitry Andric/* _LIBCPP_INLINE_VAR */ constexpr __ph<9>   _9{};
25830b57cec5SDimitry Andric/* _LIBCPP_INLINE_VAR */ constexpr __ph<10> _10{};
25840b57cec5SDimitry Andric#endif // defined(_LIBCPP_CXX03_LANG) || defined(_LIBCPP_BUILDING_LIBRARY)
25850b57cec5SDimitry Andric
25860b57cec5SDimitry Andric}  // placeholders
25870b57cec5SDimitry Andric
25880b57cec5SDimitry Andrictemplate<int _Np>
25890b57cec5SDimitry Andricstruct __is_placeholder<placeholders::__ph<_Np> >
25900b57cec5SDimitry Andric    : public integral_constant<int, _Np> {};
25910b57cec5SDimitry Andric
25920b57cec5SDimitry Andric
25930b57cec5SDimitry Andric#ifndef _LIBCPP_CXX03_LANG
25940b57cec5SDimitry Andric
25950b57cec5SDimitry Andrictemplate <class _Tp, class _Uj>
25960b57cec5SDimitry Andricinline _LIBCPP_INLINE_VISIBILITY
25970b57cec5SDimitry Andric_Tp&
25980b57cec5SDimitry Andric__mu(reference_wrapper<_Tp> __t, _Uj&)
25990b57cec5SDimitry Andric{
26000b57cec5SDimitry Andric    return __t.get();
26010b57cec5SDimitry Andric}
26020b57cec5SDimitry Andric
26030b57cec5SDimitry Andrictemplate <class _Ti, class ..._Uj, size_t ..._Indx>
26040b57cec5SDimitry Andricinline _LIBCPP_INLINE_VISIBILITY
26050b57cec5SDimitry Andrictypename __invoke_of<_Ti&, _Uj...>::type
26060b57cec5SDimitry Andric__mu_expand(_Ti& __ti, tuple<_Uj...>& __uj, __tuple_indices<_Indx...>)
26070b57cec5SDimitry Andric{
26080b57cec5SDimitry Andric    return __ti(_VSTD::forward<_Uj>(_VSTD::get<_Indx>(__uj))...);
26090b57cec5SDimitry Andric}
26100b57cec5SDimitry Andric
26110b57cec5SDimitry Andrictemplate <class _Ti, class ..._Uj>
26120b57cec5SDimitry Andricinline _LIBCPP_INLINE_VISIBILITY
26130b57cec5SDimitry Andrictypename _EnableIf
26140b57cec5SDimitry Andric<
26150b57cec5SDimitry Andric    is_bind_expression<_Ti>::value,
26160b57cec5SDimitry Andric    __invoke_of<_Ti&, _Uj...>
26170b57cec5SDimitry Andric>::type
26180b57cec5SDimitry Andric__mu(_Ti& __ti, tuple<_Uj...>& __uj)
26190b57cec5SDimitry Andric{
26200b57cec5SDimitry Andric    typedef typename __make_tuple_indices<sizeof...(_Uj)>::type __indices;
26210b57cec5SDimitry Andric    return  __mu_expand(__ti, __uj, __indices());
26220b57cec5SDimitry Andric}
26230b57cec5SDimitry Andric
26240b57cec5SDimitry Andrictemplate <bool IsPh, class _Ti, class _Uj>
26250b57cec5SDimitry Andricstruct __mu_return2 {};
26260b57cec5SDimitry Andric
26270b57cec5SDimitry Andrictemplate <class _Ti, class _Uj>
26280b57cec5SDimitry Andricstruct __mu_return2<true, _Ti, _Uj>
26290b57cec5SDimitry Andric{
26300b57cec5SDimitry Andric    typedef typename tuple_element<is_placeholder<_Ti>::value - 1, _Uj>::type type;
26310b57cec5SDimitry Andric};
26320b57cec5SDimitry Andric
26330b57cec5SDimitry Andrictemplate <class _Ti, class _Uj>
26340b57cec5SDimitry Andricinline _LIBCPP_INLINE_VISIBILITY
26350b57cec5SDimitry Andrictypename enable_if
26360b57cec5SDimitry Andric<
26370b57cec5SDimitry Andric    0 < is_placeholder<_Ti>::value,
26380b57cec5SDimitry Andric    typename __mu_return2<0 < is_placeholder<_Ti>::value, _Ti, _Uj>::type
26390b57cec5SDimitry Andric>::type
26400b57cec5SDimitry Andric__mu(_Ti&, _Uj& __uj)
26410b57cec5SDimitry Andric{
26420b57cec5SDimitry Andric    const size_t _Indx = is_placeholder<_Ti>::value - 1;
26430b57cec5SDimitry Andric    return _VSTD::forward<typename tuple_element<_Indx, _Uj>::type>(_VSTD::get<_Indx>(__uj));
26440b57cec5SDimitry Andric}
26450b57cec5SDimitry Andric
26460b57cec5SDimitry Andrictemplate <class _Ti, class _Uj>
26470b57cec5SDimitry Andricinline _LIBCPP_INLINE_VISIBILITY
26480b57cec5SDimitry Andrictypename enable_if
26490b57cec5SDimitry Andric<
26500b57cec5SDimitry Andric    !is_bind_expression<_Ti>::value &&
26510b57cec5SDimitry Andric    is_placeholder<_Ti>::value == 0 &&
26520b57cec5SDimitry Andric    !__is_reference_wrapper<_Ti>::value,
26530b57cec5SDimitry Andric    _Ti&
26540b57cec5SDimitry Andric>::type
26550b57cec5SDimitry Andric__mu(_Ti& __ti, _Uj&)
26560b57cec5SDimitry Andric{
26570b57cec5SDimitry Andric    return __ti;
26580b57cec5SDimitry Andric}
26590b57cec5SDimitry Andric
26600b57cec5SDimitry Andrictemplate <class _Ti, bool IsReferenceWrapper, bool IsBindEx, bool IsPh,
26610b57cec5SDimitry Andric          class _TupleUj>
26620b57cec5SDimitry Andricstruct __mu_return_impl;
26630b57cec5SDimitry Andric
26640b57cec5SDimitry Andrictemplate <bool _Invokable, class _Ti, class ..._Uj>
26650b57cec5SDimitry Andricstruct __mu_return_invokable  // false
26660b57cec5SDimitry Andric{
26670b57cec5SDimitry Andric    typedef __nat type;
26680b57cec5SDimitry Andric};
26690b57cec5SDimitry Andric
26700b57cec5SDimitry Andrictemplate <class _Ti, class ..._Uj>
26710b57cec5SDimitry Andricstruct __mu_return_invokable<true, _Ti, _Uj...>
26720b57cec5SDimitry Andric{
26730b57cec5SDimitry Andric    typedef typename __invoke_of<_Ti&, _Uj...>::type type;
26740b57cec5SDimitry Andric};
26750b57cec5SDimitry Andric
26760b57cec5SDimitry Andrictemplate <class _Ti, class ..._Uj>
26770b57cec5SDimitry Andricstruct __mu_return_impl<_Ti, false, true, false, tuple<_Uj...> >
26780b57cec5SDimitry Andric    : public __mu_return_invokable<__invokable<_Ti&, _Uj...>::value, _Ti, _Uj...>
26790b57cec5SDimitry Andric{
26800b57cec5SDimitry Andric};
26810b57cec5SDimitry Andric
26820b57cec5SDimitry Andrictemplate <class _Ti, class _TupleUj>
26830b57cec5SDimitry Andricstruct __mu_return_impl<_Ti, false, false, true, _TupleUj>
26840b57cec5SDimitry Andric{
26850b57cec5SDimitry Andric    typedef typename tuple_element<is_placeholder<_Ti>::value - 1,
26860b57cec5SDimitry Andric                                   _TupleUj>::type&& type;
26870b57cec5SDimitry Andric};
26880b57cec5SDimitry Andric
26890b57cec5SDimitry Andrictemplate <class _Ti, class _TupleUj>
26900b57cec5SDimitry Andricstruct __mu_return_impl<_Ti, true, false, false, _TupleUj>
26910b57cec5SDimitry Andric{
26920b57cec5SDimitry Andric    typedef typename _Ti::type& type;
26930b57cec5SDimitry Andric};
26940b57cec5SDimitry Andric
26950b57cec5SDimitry Andrictemplate <class _Ti, class _TupleUj>
26960b57cec5SDimitry Andricstruct __mu_return_impl<_Ti, false, false, false, _TupleUj>
26970b57cec5SDimitry Andric{
26980b57cec5SDimitry Andric    typedef _Ti& type;
26990b57cec5SDimitry Andric};
27000b57cec5SDimitry Andric
27010b57cec5SDimitry Andrictemplate <class _Ti, class _TupleUj>
27020b57cec5SDimitry Andricstruct __mu_return
27030b57cec5SDimitry Andric    : public __mu_return_impl<_Ti,
27040b57cec5SDimitry Andric                              __is_reference_wrapper<_Ti>::value,
27050b57cec5SDimitry Andric                              is_bind_expression<_Ti>::value,
27060b57cec5SDimitry Andric                              0 < is_placeholder<_Ti>::value &&
27070b57cec5SDimitry Andric                              is_placeholder<_Ti>::value <= tuple_size<_TupleUj>::value,
27080b57cec5SDimitry Andric                              _TupleUj>
27090b57cec5SDimitry Andric{
27100b57cec5SDimitry Andric};
27110b57cec5SDimitry Andric
27120b57cec5SDimitry Andrictemplate <class _Fp, class _BoundArgs, class _TupleUj>
27130b57cec5SDimitry Andricstruct __is_valid_bind_return
27140b57cec5SDimitry Andric{
27150b57cec5SDimitry Andric    static const bool value = false;
27160b57cec5SDimitry Andric};
27170b57cec5SDimitry Andric
27180b57cec5SDimitry Andrictemplate <class _Fp, class ..._BoundArgs, class _TupleUj>
27190b57cec5SDimitry Andricstruct __is_valid_bind_return<_Fp, tuple<_BoundArgs...>, _TupleUj>
27200b57cec5SDimitry Andric{
27210b57cec5SDimitry Andric    static const bool value = __invokable<_Fp,
27220b57cec5SDimitry Andric                    typename __mu_return<_BoundArgs, _TupleUj>::type...>::value;
27230b57cec5SDimitry Andric};
27240b57cec5SDimitry Andric
27250b57cec5SDimitry Andrictemplate <class _Fp, class ..._BoundArgs, class _TupleUj>
27260b57cec5SDimitry Andricstruct __is_valid_bind_return<_Fp, const tuple<_BoundArgs...>, _TupleUj>
27270b57cec5SDimitry Andric{
27280b57cec5SDimitry Andric    static const bool value = __invokable<_Fp,
27290b57cec5SDimitry Andric                    typename __mu_return<const _BoundArgs, _TupleUj>::type...>::value;
27300b57cec5SDimitry Andric};
27310b57cec5SDimitry Andric
27320b57cec5SDimitry Andrictemplate <class _Fp, class _BoundArgs, class _TupleUj,
27330b57cec5SDimitry Andric          bool = __is_valid_bind_return<_Fp, _BoundArgs, _TupleUj>::value>
27340b57cec5SDimitry Andricstruct __bind_return;
27350b57cec5SDimitry Andric
27360b57cec5SDimitry Andrictemplate <class _Fp, class ..._BoundArgs, class _TupleUj>
27370b57cec5SDimitry Andricstruct __bind_return<_Fp, tuple<_BoundArgs...>, _TupleUj, true>
27380b57cec5SDimitry Andric{
27390b57cec5SDimitry Andric    typedef typename __invoke_of
27400b57cec5SDimitry Andric    <
27410b57cec5SDimitry Andric        _Fp&,
27420b57cec5SDimitry Andric        typename __mu_return
27430b57cec5SDimitry Andric        <
27440b57cec5SDimitry Andric            _BoundArgs,
27450b57cec5SDimitry Andric            _TupleUj
27460b57cec5SDimitry Andric        >::type...
27470b57cec5SDimitry Andric    >::type type;
27480b57cec5SDimitry Andric};
27490b57cec5SDimitry Andric
27500b57cec5SDimitry Andrictemplate <class _Fp, class ..._BoundArgs, class _TupleUj>
27510b57cec5SDimitry Andricstruct __bind_return<_Fp, const tuple<_BoundArgs...>, _TupleUj, true>
27520b57cec5SDimitry Andric{
27530b57cec5SDimitry Andric    typedef typename __invoke_of
27540b57cec5SDimitry Andric    <
27550b57cec5SDimitry Andric        _Fp&,
27560b57cec5SDimitry Andric        typename __mu_return
27570b57cec5SDimitry Andric        <
27580b57cec5SDimitry Andric            const _BoundArgs,
27590b57cec5SDimitry Andric            _TupleUj
27600b57cec5SDimitry Andric        >::type...
27610b57cec5SDimitry Andric    >::type type;
27620b57cec5SDimitry Andric};
27630b57cec5SDimitry Andric
27640b57cec5SDimitry Andrictemplate <class _Fp, class _BoundArgs, size_t ..._Indx, class _Args>
27650b57cec5SDimitry Andricinline _LIBCPP_INLINE_VISIBILITY
27660b57cec5SDimitry Andrictypename __bind_return<_Fp, _BoundArgs, _Args>::type
27670b57cec5SDimitry Andric__apply_functor(_Fp& __f, _BoundArgs& __bound_args, __tuple_indices<_Indx...>,
27680b57cec5SDimitry Andric                _Args&& __args)
27690b57cec5SDimitry Andric{
27700b57cec5SDimitry Andric    return _VSTD::__invoke(__f, _VSTD::__mu(_VSTD::get<_Indx>(__bound_args), __args)...);
27710b57cec5SDimitry Andric}
27720b57cec5SDimitry Andric
27730b57cec5SDimitry Andrictemplate<class _Fp, class ..._BoundArgs>
27740b57cec5SDimitry Andricclass __bind
27750b57cec5SDimitry Andric    : public __weak_result_type<typename decay<_Fp>::type>
27760b57cec5SDimitry Andric{
27770b57cec5SDimitry Andricprotected:
27780b57cec5SDimitry Andric    typedef typename decay<_Fp>::type _Fd;
27790b57cec5SDimitry Andric    typedef tuple<typename decay<_BoundArgs>::type...> _Td;
27800b57cec5SDimitry Andricprivate:
27810b57cec5SDimitry Andric    _Fd __f_;
27820b57cec5SDimitry Andric    _Td __bound_args_;
27830b57cec5SDimitry Andric
27840b57cec5SDimitry Andric    typedef typename __make_tuple_indices<sizeof...(_BoundArgs)>::type __indices;
27850b57cec5SDimitry Andricpublic:
27860b57cec5SDimitry Andric    template <class _Gp, class ..._BA,
27870b57cec5SDimitry Andric              class = typename enable_if
27880b57cec5SDimitry Andric                               <
27890b57cec5SDimitry Andric                                  is_constructible<_Fd, _Gp>::value &&
27900b57cec5SDimitry Andric                                  !is_same<typename remove_reference<_Gp>::type,
27910b57cec5SDimitry Andric                                           __bind>::value
27920b57cec5SDimitry Andric                               >::type>
27930b57cec5SDimitry Andric      _LIBCPP_INLINE_VISIBILITY
27940b57cec5SDimitry Andric      explicit __bind(_Gp&& __f, _BA&& ...__bound_args)
27950b57cec5SDimitry Andric        : __f_(_VSTD::forward<_Gp>(__f)),
27960b57cec5SDimitry Andric          __bound_args_(_VSTD::forward<_BA>(__bound_args)...) {}
27970b57cec5SDimitry Andric
27980b57cec5SDimitry Andric    template <class ..._Args>
27990b57cec5SDimitry Andric        _LIBCPP_INLINE_VISIBILITY
28000b57cec5SDimitry Andric        typename __bind_return<_Fd, _Td, tuple<_Args&&...> >::type
28010b57cec5SDimitry Andric        operator()(_Args&& ...__args)
28020b57cec5SDimitry Andric        {
28030b57cec5SDimitry Andric            return _VSTD::__apply_functor(__f_, __bound_args_, __indices(),
28040b57cec5SDimitry Andric                                  tuple<_Args&&...>(_VSTD::forward<_Args>(__args)...));
28050b57cec5SDimitry Andric        }
28060b57cec5SDimitry Andric
28070b57cec5SDimitry Andric    template <class ..._Args>
28080b57cec5SDimitry Andric        _LIBCPP_INLINE_VISIBILITY
28090b57cec5SDimitry Andric        typename __bind_return<const _Fd, const _Td, tuple<_Args&&...> >::type
28100b57cec5SDimitry Andric        operator()(_Args&& ...__args) const
28110b57cec5SDimitry Andric        {
28120b57cec5SDimitry Andric            return _VSTD::__apply_functor(__f_, __bound_args_, __indices(),
28130b57cec5SDimitry Andric                                   tuple<_Args&&...>(_VSTD::forward<_Args>(__args)...));
28140b57cec5SDimitry Andric        }
28150b57cec5SDimitry Andric};
28160b57cec5SDimitry Andric
28170b57cec5SDimitry Andrictemplate<class _Fp, class ..._BoundArgs>
28180b57cec5SDimitry Andricstruct __is_bind_expression<__bind<_Fp, _BoundArgs...> > : public true_type {};
28190b57cec5SDimitry Andric
28200b57cec5SDimitry Andrictemplate<class _Rp, class _Fp, class ..._BoundArgs>
28210b57cec5SDimitry Andricclass __bind_r
28220b57cec5SDimitry Andric    : public __bind<_Fp, _BoundArgs...>
28230b57cec5SDimitry Andric{
28240b57cec5SDimitry Andric    typedef __bind<_Fp, _BoundArgs...> base;
28250b57cec5SDimitry Andric    typedef typename base::_Fd _Fd;
28260b57cec5SDimitry Andric    typedef typename base::_Td _Td;
28270b57cec5SDimitry Andricpublic:
28280b57cec5SDimitry Andric    typedef _Rp result_type;
28290b57cec5SDimitry Andric
28300b57cec5SDimitry Andric
28310b57cec5SDimitry Andric    template <class _Gp, class ..._BA,
28320b57cec5SDimitry Andric              class = typename enable_if
28330b57cec5SDimitry Andric                               <
28340b57cec5SDimitry Andric                                  is_constructible<_Fd, _Gp>::value &&
28350b57cec5SDimitry Andric                                  !is_same<typename remove_reference<_Gp>::type,
28360b57cec5SDimitry Andric                                           __bind_r>::value
28370b57cec5SDimitry Andric                               >::type>
28380b57cec5SDimitry Andric      _LIBCPP_INLINE_VISIBILITY
28390b57cec5SDimitry Andric      explicit __bind_r(_Gp&& __f, _BA&& ...__bound_args)
28400b57cec5SDimitry Andric        : base(_VSTD::forward<_Gp>(__f),
28410b57cec5SDimitry Andric               _VSTD::forward<_BA>(__bound_args)...) {}
28420b57cec5SDimitry Andric
28430b57cec5SDimitry Andric    template <class ..._Args>
28440b57cec5SDimitry Andric        _LIBCPP_INLINE_VISIBILITY
28450b57cec5SDimitry Andric        typename enable_if
28460b57cec5SDimitry Andric        <
28470b57cec5SDimitry Andric            is_convertible<typename __bind_return<_Fd, _Td, tuple<_Args&&...> >::type,
28480b57cec5SDimitry Andric                           result_type>::value || is_void<_Rp>::value,
28490b57cec5SDimitry Andric            result_type
28500b57cec5SDimitry Andric        >::type
28510b57cec5SDimitry Andric        operator()(_Args&& ...__args)
28520b57cec5SDimitry Andric        {
28530b57cec5SDimitry Andric            typedef __invoke_void_return_wrapper<_Rp> _Invoker;
28540b57cec5SDimitry Andric            return _Invoker::__call(static_cast<base&>(*this), _VSTD::forward<_Args>(__args)...);
28550b57cec5SDimitry Andric        }
28560b57cec5SDimitry Andric
28570b57cec5SDimitry Andric    template <class ..._Args>
28580b57cec5SDimitry Andric        _LIBCPP_INLINE_VISIBILITY
28590b57cec5SDimitry Andric        typename enable_if
28600b57cec5SDimitry Andric        <
28610b57cec5SDimitry Andric            is_convertible<typename __bind_return<const _Fd, const _Td, tuple<_Args&&...> >::type,
28620b57cec5SDimitry Andric                           result_type>::value || is_void<_Rp>::value,
28630b57cec5SDimitry Andric            result_type
28640b57cec5SDimitry Andric        >::type
28650b57cec5SDimitry Andric        operator()(_Args&& ...__args) const
28660b57cec5SDimitry Andric        {
28670b57cec5SDimitry Andric            typedef __invoke_void_return_wrapper<_Rp> _Invoker;
28680b57cec5SDimitry Andric            return _Invoker::__call(static_cast<base const&>(*this), _VSTD::forward<_Args>(__args)...);
28690b57cec5SDimitry Andric        }
28700b57cec5SDimitry Andric};
28710b57cec5SDimitry Andric
28720b57cec5SDimitry Andrictemplate<class _Rp, class _Fp, class ..._BoundArgs>
28730b57cec5SDimitry Andricstruct __is_bind_expression<__bind_r<_Rp, _Fp, _BoundArgs...> > : public true_type {};
28740b57cec5SDimitry Andric
28750b57cec5SDimitry Andrictemplate<class _Fp, class ..._BoundArgs>
28760b57cec5SDimitry Andricinline _LIBCPP_INLINE_VISIBILITY
28770b57cec5SDimitry Andric__bind<_Fp, _BoundArgs...>
28780b57cec5SDimitry Andricbind(_Fp&& __f, _BoundArgs&&... __bound_args)
28790b57cec5SDimitry Andric{
28800b57cec5SDimitry Andric    typedef __bind<_Fp, _BoundArgs...> type;
28810b57cec5SDimitry Andric    return type(_VSTD::forward<_Fp>(__f), _VSTD::forward<_BoundArgs>(__bound_args)...);
28820b57cec5SDimitry Andric}
28830b57cec5SDimitry Andric
28840b57cec5SDimitry Andrictemplate<class _Rp, class _Fp, class ..._BoundArgs>
28850b57cec5SDimitry Andricinline _LIBCPP_INLINE_VISIBILITY
28860b57cec5SDimitry Andric__bind_r<_Rp, _Fp, _BoundArgs...>
28870b57cec5SDimitry Andricbind(_Fp&& __f, _BoundArgs&&... __bound_args)
28880b57cec5SDimitry Andric{
28890b57cec5SDimitry Andric    typedef __bind_r<_Rp, _Fp, _BoundArgs...> type;
28900b57cec5SDimitry Andric    return type(_VSTD::forward<_Fp>(__f), _VSTD::forward<_BoundArgs>(__bound_args)...);
28910b57cec5SDimitry Andric}
28920b57cec5SDimitry Andric
28930b57cec5SDimitry Andric#endif  // _LIBCPP_CXX03_LANG
28940b57cec5SDimitry Andric
28950b57cec5SDimitry Andric#if _LIBCPP_STD_VER > 14
28960b57cec5SDimitry Andric
28970b57cec5SDimitry Andrictemplate <class _Fn, class ..._Args>
28980b57cec5SDimitry Andricinvoke_result_t<_Fn, _Args...>
28990b57cec5SDimitry Andricinvoke(_Fn&& __f, _Args&&... __args)
29000b57cec5SDimitry Andric    noexcept(is_nothrow_invocable_v<_Fn, _Args...>)
29010b57cec5SDimitry Andric{
29020b57cec5SDimitry Andric    return _VSTD::__invoke(_VSTD::forward<_Fn>(__f), _VSTD::forward<_Args>(__args)...);
29030b57cec5SDimitry Andric}
29040b57cec5SDimitry Andric
29050b57cec5SDimitry Andrictemplate <class _DecayFunc>
29060b57cec5SDimitry Andricclass _LIBCPP_TEMPLATE_VIS __not_fn_imp {
29070b57cec5SDimitry Andric  _DecayFunc __fd;
29080b57cec5SDimitry Andric
29090b57cec5SDimitry Andricpublic:
29100b57cec5SDimitry Andric    __not_fn_imp() = delete;
29110b57cec5SDimitry Andric
29120b57cec5SDimitry Andric    template <class ..._Args>
29130b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
29140b57cec5SDimitry Andric    auto operator()(_Args&& ...__args) &
29150b57cec5SDimitry Andric            noexcept(noexcept(!_VSTD::invoke(__fd, _VSTD::forward<_Args>(__args)...)))
29160b57cec5SDimitry Andric        -> decltype(          !_VSTD::invoke(__fd, _VSTD::forward<_Args>(__args)...))
29170b57cec5SDimitry Andric        { return              !_VSTD::invoke(__fd, _VSTD::forward<_Args>(__args)...); }
29180b57cec5SDimitry Andric
29190b57cec5SDimitry Andric    template <class ..._Args>
29200b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
29210b57cec5SDimitry Andric    auto operator()(_Args&& ...__args) &&
29220b57cec5SDimitry Andric            noexcept(noexcept(!_VSTD::invoke(_VSTD::move(__fd), _VSTD::forward<_Args>(__args)...)))
29230b57cec5SDimitry Andric        -> decltype(          !_VSTD::invoke(_VSTD::move(__fd), _VSTD::forward<_Args>(__args)...))
29240b57cec5SDimitry Andric        { return              !_VSTD::invoke(_VSTD::move(__fd), _VSTD::forward<_Args>(__args)...); }
29250b57cec5SDimitry Andric
29260b57cec5SDimitry Andric    template <class ..._Args>
29270b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
29280b57cec5SDimitry Andric    auto operator()(_Args&& ...__args) const&
29290b57cec5SDimitry Andric            noexcept(noexcept(!_VSTD::invoke(__fd, _VSTD::forward<_Args>(__args)...)))
29300b57cec5SDimitry Andric        -> decltype(          !_VSTD::invoke(__fd, _VSTD::forward<_Args>(__args)...))
29310b57cec5SDimitry Andric        { return              !_VSTD::invoke(__fd, _VSTD::forward<_Args>(__args)...); }
29320b57cec5SDimitry Andric
29330b57cec5SDimitry Andric
29340b57cec5SDimitry Andric    template <class ..._Args>
29350b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
29360b57cec5SDimitry Andric    auto operator()(_Args&& ...__args) const&&
29370b57cec5SDimitry Andric            noexcept(noexcept(!_VSTD::invoke(_VSTD::move(__fd), _VSTD::forward<_Args>(__args)...)))
29380b57cec5SDimitry Andric        -> decltype(          !_VSTD::invoke(_VSTD::move(__fd), _VSTD::forward<_Args>(__args)...))
29390b57cec5SDimitry Andric        { return              !_VSTD::invoke(_VSTD::move(__fd), _VSTD::forward<_Args>(__args)...); }
29400b57cec5SDimitry Andric
29410b57cec5SDimitry Andricprivate:
29420b57cec5SDimitry Andric    template <class _RawFunc,
29430b57cec5SDimitry Andric              class = enable_if_t<!is_same<decay_t<_RawFunc>, __not_fn_imp>::value>>
29440b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
29450b57cec5SDimitry Andric    explicit __not_fn_imp(_RawFunc&& __rf)
29460b57cec5SDimitry Andric        : __fd(_VSTD::forward<_RawFunc>(__rf)) {}
29470b57cec5SDimitry Andric
29480b57cec5SDimitry Andric    template <class _RawFunc>
29490b57cec5SDimitry Andric    friend inline _LIBCPP_INLINE_VISIBILITY
29500b57cec5SDimitry Andric    __not_fn_imp<decay_t<_RawFunc>> not_fn(_RawFunc&&);
29510b57cec5SDimitry Andric};
29520b57cec5SDimitry Andric
29530b57cec5SDimitry Andrictemplate <class _RawFunc>
29540b57cec5SDimitry Andricinline _LIBCPP_INLINE_VISIBILITY
29550b57cec5SDimitry Andric__not_fn_imp<decay_t<_RawFunc>> not_fn(_RawFunc&& __fn) {
29560b57cec5SDimitry Andric    return __not_fn_imp<decay_t<_RawFunc>>(_VSTD::forward<_RawFunc>(__fn));
29570b57cec5SDimitry Andric}
29580b57cec5SDimitry Andric
29590b57cec5SDimitry Andric#endif
29600b57cec5SDimitry Andric
29610b57cec5SDimitry Andric// struct hash<T*> in <memory>
29620b57cec5SDimitry Andric
29630b57cec5SDimitry Andrictemplate <class _BinaryPredicate, class _ForwardIterator1, class _ForwardIterator2>
29640b57cec5SDimitry Andricpair<_ForwardIterator1, _ForwardIterator1> _LIBCPP_CONSTEXPR_AFTER_CXX11
29650b57cec5SDimitry Andric__search(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
29660b57cec5SDimitry Andric         _ForwardIterator2 __first2, _ForwardIterator2 __last2, _BinaryPredicate __pred,
29670b57cec5SDimitry Andric         forward_iterator_tag, forward_iterator_tag)
29680b57cec5SDimitry Andric{
29690b57cec5SDimitry Andric    if (__first2 == __last2)
29700b57cec5SDimitry Andric        return make_pair(__first1, __first1);  // Everything matches an empty sequence
29710b57cec5SDimitry Andric    while (true)
29720b57cec5SDimitry Andric    {
29730b57cec5SDimitry Andric        // Find first element in sequence 1 that matchs *__first2, with a mininum of loop checks
29740b57cec5SDimitry Andric        while (true)
29750b57cec5SDimitry Andric        {
29760b57cec5SDimitry Andric            if (__first1 == __last1)  // return __last1 if no element matches *__first2
29770b57cec5SDimitry Andric                return make_pair(__last1, __last1);
29780b57cec5SDimitry Andric            if (__pred(*__first1, *__first2))
29790b57cec5SDimitry Andric                break;
29800b57cec5SDimitry Andric            ++__first1;
29810b57cec5SDimitry Andric        }
29820b57cec5SDimitry Andric        // *__first1 matches *__first2, now match elements after here
29830b57cec5SDimitry Andric        _ForwardIterator1 __m1 = __first1;
29840b57cec5SDimitry Andric        _ForwardIterator2 __m2 = __first2;
29850b57cec5SDimitry Andric        while (true)
29860b57cec5SDimitry Andric        {
29870b57cec5SDimitry Andric            if (++__m2 == __last2)  // If pattern exhausted, __first1 is the answer (works for 1 element pattern)
29880b57cec5SDimitry Andric                return make_pair(__first1, __m1);
29890b57cec5SDimitry Andric            if (++__m1 == __last1)  // Otherwise if source exhaused, pattern not found
29900b57cec5SDimitry Andric                return make_pair(__last1, __last1);
29910b57cec5SDimitry Andric            if (!__pred(*__m1, *__m2))  // if there is a mismatch, restart with a new __first1
29920b57cec5SDimitry Andric            {
29930b57cec5SDimitry Andric                ++__first1;
29940b57cec5SDimitry Andric                break;
29950b57cec5SDimitry Andric            }  // else there is a match, check next elements
29960b57cec5SDimitry Andric        }
29970b57cec5SDimitry Andric    }
29980b57cec5SDimitry Andric}
29990b57cec5SDimitry Andric
30000b57cec5SDimitry Andrictemplate <class _BinaryPredicate, class _RandomAccessIterator1, class _RandomAccessIterator2>
30010b57cec5SDimitry Andric_LIBCPP_CONSTEXPR_AFTER_CXX11
30020b57cec5SDimitry Andricpair<_RandomAccessIterator1, _RandomAccessIterator1>
30030b57cec5SDimitry Andric__search(_RandomAccessIterator1 __first1, _RandomAccessIterator1 __last1,
30040b57cec5SDimitry Andric         _RandomAccessIterator2 __first2, _RandomAccessIterator2 __last2, _BinaryPredicate __pred,
30050b57cec5SDimitry Andric           random_access_iterator_tag, random_access_iterator_tag)
30060b57cec5SDimitry Andric{
30070b57cec5SDimitry Andric    typedef typename iterator_traits<_RandomAccessIterator1>::difference_type _D1;
30080b57cec5SDimitry Andric    typedef typename iterator_traits<_RandomAccessIterator2>::difference_type _D2;
30090b57cec5SDimitry Andric    // Take advantage of knowing source and pattern lengths.  Stop short when source is smaller than pattern
30100b57cec5SDimitry Andric    const _D2 __len2 = __last2 - __first2;
30110b57cec5SDimitry Andric    if (__len2 == 0)
30120b57cec5SDimitry Andric        return make_pair(__first1, __first1);
30130b57cec5SDimitry Andric    const _D1 __len1 = __last1 - __first1;
30140b57cec5SDimitry Andric    if (__len1 < __len2)
30150b57cec5SDimitry Andric        return make_pair(__last1, __last1);
30160b57cec5SDimitry Andric    const _RandomAccessIterator1 __s = __last1 - (__len2 - 1);  // Start of pattern match can't go beyond here
30170b57cec5SDimitry Andric
30180b57cec5SDimitry Andric    while (true)
30190b57cec5SDimitry Andric    {
30200b57cec5SDimitry Andric        while (true)
30210b57cec5SDimitry Andric        {
30220b57cec5SDimitry Andric            if (__first1 == __s)
30230b57cec5SDimitry Andric                return make_pair(__last1, __last1);
30240b57cec5SDimitry Andric            if (__pred(*__first1, *__first2))
30250b57cec5SDimitry Andric                break;
30260b57cec5SDimitry Andric            ++__first1;
30270b57cec5SDimitry Andric        }
30280b57cec5SDimitry Andric
30290b57cec5SDimitry Andric        _RandomAccessIterator1 __m1 = __first1;
30300b57cec5SDimitry Andric        _RandomAccessIterator2 __m2 = __first2;
30310b57cec5SDimitry Andric         while (true)
30320b57cec5SDimitry Andric         {
30330b57cec5SDimitry Andric             if (++__m2 == __last2)
30340b57cec5SDimitry Andric                 return make_pair(__first1, __first1 + __len2);
30350b57cec5SDimitry Andric             ++__m1;          // no need to check range on __m1 because __s guarantees we have enough source
30360b57cec5SDimitry Andric             if (!__pred(*__m1, *__m2))
30370b57cec5SDimitry Andric             {
30380b57cec5SDimitry Andric                 ++__first1;
30390b57cec5SDimitry Andric                 break;
30400b57cec5SDimitry Andric             }
30410b57cec5SDimitry Andric         }
30420b57cec5SDimitry Andric    }
30430b57cec5SDimitry Andric}
30440b57cec5SDimitry Andric
30450b57cec5SDimitry Andric#if _LIBCPP_STD_VER > 14
30460b57cec5SDimitry Andric
30470b57cec5SDimitry Andric// default searcher
30480b57cec5SDimitry Andrictemplate<class _ForwardIterator, class _BinaryPredicate = equal_to<>>
30490b57cec5SDimitry Andricclass _LIBCPP_TYPE_VIS default_searcher {
30500b57cec5SDimitry Andricpublic:
30510b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
30520b57cec5SDimitry Andric    default_searcher(_ForwardIterator __f, _ForwardIterator __l,
30530b57cec5SDimitry Andric                       _BinaryPredicate __p = _BinaryPredicate())
30540b57cec5SDimitry Andric        : __first_(__f), __last_(__l), __pred_(__p) {}
30550b57cec5SDimitry Andric
30560b57cec5SDimitry Andric    template <typename _ForwardIterator2>
30570b57cec5SDimitry Andric    _LIBCPP_INLINE_VISIBILITY
30580b57cec5SDimitry Andric    pair<_ForwardIterator2, _ForwardIterator2>
30590b57cec5SDimitry Andric    operator () (_ForwardIterator2 __f, _ForwardIterator2 __l) const
30600b57cec5SDimitry Andric    {
30610b57cec5SDimitry Andric        return _VSTD::__search(__f, __l, __first_, __last_, __pred_,
30620b57cec5SDimitry Andric            typename _VSTD::iterator_traits<_ForwardIterator>::iterator_category(),
30630b57cec5SDimitry Andric            typename _VSTD::iterator_traits<_ForwardIterator2>::iterator_category());
30640b57cec5SDimitry Andric    }
30650b57cec5SDimitry Andric
30660b57cec5SDimitry Andricprivate:
30670b57cec5SDimitry Andric    _ForwardIterator __first_;
30680b57cec5SDimitry Andric    _ForwardIterator __last_;
30690b57cec5SDimitry Andric    _BinaryPredicate __pred_;
30700b57cec5SDimitry Andric    };
30710b57cec5SDimitry Andric
30720b57cec5SDimitry Andric#endif // _LIBCPP_STD_VER > 14
30730b57cec5SDimitry Andric
30740b57cec5SDimitry Andric#if _LIBCPP_STD_VER > 17
30750b57cec5SDimitry Andrictemplate <class _Tp>
30760b57cec5SDimitry Andricusing unwrap_reference_t = typename unwrap_reference<_Tp>::type;
30770b57cec5SDimitry Andric
30780b57cec5SDimitry Andrictemplate <class _Tp>
30790b57cec5SDimitry Andricusing unwrap_ref_decay_t = typename unwrap_ref_decay<_Tp>::type;
30800b57cec5SDimitry Andric#endif // > C++17
30810b57cec5SDimitry Andric
30820b57cec5SDimitry Andrictemplate <class _Container, class _Predicate>
30830b57cec5SDimitry Andricinline void __libcpp_erase_if_container( _Container& __c, _Predicate __pred)
30840b57cec5SDimitry Andric{
30850b57cec5SDimitry Andric	for (typename _Container::iterator __iter = __c.begin(), __last = __c.end(); __iter != __last;)
30860b57cec5SDimitry Andric	{
30870b57cec5SDimitry Andric		if (__pred(*__iter))
30880b57cec5SDimitry Andric			__iter = __c.erase(__iter);
30890b57cec5SDimitry Andric		else
30900b57cec5SDimitry Andric			++__iter;
30910b57cec5SDimitry Andric	}
30920b57cec5SDimitry Andric}
30930b57cec5SDimitry Andric
30940b57cec5SDimitry Andric_LIBCPP_END_NAMESPACE_STD
30950b57cec5SDimitry Andric
30960b57cec5SDimitry Andric#endif  // _LIBCPP_FUNCTIONAL
3097