标准文档 中当前头文件是语言支持库(Language support library)中的部分内容,为了更好的表述其内容,本手册将其移至通用工具库(General utilities library)中的 函数对象 主题中。

语言支持库(参考手册) ​语言支持库(标准)

通用工具库(参考手册) ​通用工具库(标准)

  • function C++11 将任意类型的可调用(Callable)对象与函数调用的特征封装到一起。
    mem_fn C++11 将成员函数(Member function)转化成函数对象(指针(Pointer)版)
    bad_function_call C++11 这是一个异常,当调用一个空的 function 对象时抛出
    is_bind_expression C++11 特征类,识别指定类型是否是一个捆绑表达式
    is_placeholder C++11 特征类,识别指定类型是否是一个捆绑表达或中的位置标置符(Bind placeholde)
    reference_wrapper C++11 可拷贝构造(CopyConstructible)可赋值构造(CopyAssignable)的引用封装

    哈希

    hash 哈希函数对象

    hash 函数对内置类型的特例化

    template<> struct hash<bool>;
    template<> struct hash<char>;
    template<> struct hash<signed char>;
    template<> struct hash<unsigned char>;
    template<> struct hash<char16_t>;
    template<> struct hash<char32_t>;
    template<> struct hash<wchar_t>;
    template<> struct hash<short>;
    template<> struct hash<unsigned short>;
    template<> struct hash<int>;
    template<> struct hash<unsigned int>;
    template<> struct hash<long>;
    template<> struct hash<long long>;
    template<> struct hash<unsigned long>;
    template<> struct hash<unsigned long long>;
    template<> struct hash<float>;
    template<> struct hash<double>;
    template<> struct hash<long double>;
    template< class T > struct hash<T*>;
  • bind C++11 将一至多个参数绑定到函数对象
    ref C++11 用从参数推导出的类型创建一个 reference_wrapper 对象
    cref C++11 用从参数推导出的类型创建一个 reference_wrapper 对象
  • 算述操作

    plus 加法函数对象,等价于 x + y
    minus 减法函数对象,等价于 x - y
    multiplies 乘法函数对象,等价于 x * y
    divides 除法函数对象,等价于 x / y
    modulus 取模函数对象,等价于 x % y
    negate 取相反数函数对象,等价于 -x

    比较

    equal_to 函数对象,等价于 x == y
    not_equal_to 函数对象,等价于 x != y
    greater 函数对象,等价于 x > y
    less 函数对象,等价于 x < y
    greater_equal 函数对象,等价于 x >= y
    less_equal 函数对象,等价于 x <= y

    逻辑操作

    logical_and 函数对象,等价于 x && y
    logical_or 函数对象,等价于 x || y
    logical_not 函数对象,等价于 !x

    按位操作

    bit_and 函数对象,等价于 x & y
    bit_or 函数对象,等价于 x | y
    bit_xor 函数对象,等价于 x ^ y

    否认操作

    unary_negate 封装函数对象,返回一元谓词(Unary predicate)所表示范围的补(Complement)
    binary_negate 封装函数对象,返回二元谓词所表示范围的补
    not1 构造自定义 unary_negate 对象
    not2 构造自定义 binary_negate 对象
  • 基类

    unary_function 已弃用 用于创建带一个参数的函数对象的基类
    binary_function 已弃用 用于创建带两个参数的函数对象的基类

    绑定器

    binder1st 已弃用 将一个参数绑定到一个二元函数后获得的一元函数对象
    binder2nd 已弃用
    bind1st 已弃用 将一个参数绑定到一个二元函数
    bind2nd 已弃用

    函数适配器

    pointer_to_unary_function 已弃用 通过一个指针生成一个一元函数对象
    pointer_to_binary_function 已弃用 通过一个指针生成一个二元函数对象
    ptr_fun 已弃用 将一个函数指针转化成函数对象
    mem_fun_t 已弃用 通过一个无参数成员函数生成一个函数对象(指针版)
    mem_fun1_t 已弃用 通过一个带一个参数的成员函数生成一个函数对象(指针版)
    const_mem_fun_t 已弃用 通过一个 const 限定的无参数成员函数生成一个函数对象(指针版)
    const_mem_fun1_t 已弃用 通过一个 const 限定的带一个参数的成员函数生成一个函数对象(指针版)
    mem_fun 已弃用 将一个成员函数转化成函数对象(指针版)
    mem_fun_ref_t 已弃用 通过一个无参数成员函数生成一个函数对象(引用版)
    mem_fun1_ref_t 已弃用 通过一个带一个参数的成员函数生成一个函数对象(引用版)
    const_mem_fun_ref_t 已弃用 通过一个 const 限定的无参数成员函数生成一个函数对象(引用版)
    const_mem_fun1_ref_t 已弃用 通过一个 const 限定的带一个参数的成员函数生成一个函数对象(引用版)
    mem_fun_ref 已弃用 将成员函数转化成函数对象(引用(Reference)版)
  • namespace std {
     
        // base (deprecated):
        template <class Arg, class Result> struct unary_function;
        template <class Arg1, class Arg2, class Result> struct binary_function;
     
        // reference_wrapper:
        template <class T> class reference_wrapper;
        template <class T> reference_wrapper<T> ref(T&) noexcept;
        template <class T> reference_wrapper<const T> cref(const T&) noexcept;
        template <class T> void ref(const T&&) = delete;
        template <class T> void cref(const T&&) = delete;
        template <class T> reference_wrapper<T> ref(reference_wrapper<T>) noexcept;
        template <class T> reference_wrapper<const T> cref(reference_wrapper<T>) noexcept;
     
        // arithmetic operations:
        template <class T> struct plus;
        template <class T> struct minus;
        template <class T> struct multiplies;
        template <class T> struct divides;
        template <class T> struct modulus;
        template <class T> struct negate;
     
        // comparisons:
        template <class T> struct equal_to;
        template <class T> struct not_equal_to;
        template <class T> struct greater;
        template <class T> struct less;
        template <class T> struct greater_equal;
        template <class T> struct less_equal;
     
        // logical operations:
        template <class T> struct logical_and;
        template <class T> struct logical_or;
        template <class T> struct logical_not;
     
        // bitwise operations:
        template <class T> struct bit_and;
        template <class T> struct bit_or;
        template <class T> struct bit_xor;
     
        // negators:
        template <class Predicate> class unary_negate;
        template <class Predicate>
            unary_negate<Predicate> 
            not1(const Predicate&);
        template <class Predicate>  class binary_negate;
        template <class Predicate>
            binary_negate<Predicate>
            not2(const Predicate&);
     
        // bind:
        template<class T> struct is_bind_expression;
        template<class T> struct is_placeholder;
        template<class  F, class... BoundArgs>
            /*unspecified*/ bind(F&&, BoundArgs&&...);
        template<class  R, class F, class... BoundArgs>
            /*unspecified*/ bind(F&&, BoundArgs&&...);
     
        namespace placeholders {
            // M is the implementation-defined number of placeholders
            extern /*unspecified*/ _1;
            extern /*unspecified*/ _2;
            .
            .
            .
            extern /*unspecified*/ _M;
        }
     
        // binders (deprecated):
        template <class Fn> class binder1st;
        template <class Fn, class T>
            binder1st<Fn> bind1st(const Fn&, const T&);
        template <class Fn> class binder2nd;
        template <class Fn, class T>
            binder2nd<Fn> bind2nd(const Fn&, const T&);
     
        // adaptors (deprecated):
        template <class Arg, class Result> class pointer_to_unary_function;
        template <class Arg, class Result>
            pointer_to_unary_function<Arg,Result> ptr_fun(Result (*)(Arg));
        template <class Arg1, class Arg2, class Result>
            class pointer_to_binary_function;
        template <class Arg1, class Arg2, class Result>
            pointer_to_binary_function<Arg1,Arg2,Result>
            ptr_fun(Result (*)(Arg1,Arg2));
     
        // adaptors (deprecated):
        template<class S, class T> class mem_fun_t;
        template<class S, class T, class A> class mem_fun1_t;
        template<class S, class T>
            mem_fun_t<S,T> mem_fun(S (T::*f)());
        template<class S, class T, class A>
            mem_fun1_t<S,T,A> mem_fun(S (T::*f)(A));
        template<class S, class T> class mem_fun_ref_t;
        template<class S, class T, class A> class mem_fun1_ref_t;
        template<class S, class T>
            mem_fun_ref_t<S,T> mem_fun_ref(S (T::*f)());
        template<class S, class T, class A>
            mem_fun1_ref_t<S,T,A> mem_fun_ref(S (T::*f)(A));
        template <class S, class T> class const_mem_fun_t;
        template <class S, class T, class A> class const_mem_fun1_t;
        template <class S, class T>
            const_mem_fun_t<S,T> mem_fun(S (T::*f)() const);
        template <class S, class T, class A>
            const_mem_fun1_t<S,T,A> mem_fun(S (T::*f)(A) const);
        template <class S, class T> class const_mem_fun_ref_t;
        template <class S, class T, class A> class const_mem_fun1_ref_t;
        template <class S, class T>
            const_mem_fun_ref_t<S,T> mem_fun_ref(S (T::*f)() const);
        template <class S, class T, class A>
            const_mem_fun1_ref_t<S,T,A> mem_fun_ref(S (T::*f)(A) const);
     
        // member function adaptors:
        template<class R, class T> /*unspecified*/ mem_fn(R T::*);
        template<class R, class T, class... Args>
            /*unspecified*/ mem_fn(R (T::*)(Args...));
        template<class R, class T, class... Args>
            /*unspecified*/ mem_fn(R (T::*)(Args...) const);
        template<class R, class T, class... Args>
            /*unspecified*/ mem_fn(R (T::*)(Args...) volatile);
        template<class R, class T, class... Args>
            /*unspecified*/ mem_fn(R (T::*)(Args...) const volatile);
        template<class R, class T, class... Args>
            /*unspecified*/ mem_fn(R (T::*)(Args...) &);
        template<class R, class T, class... Args>
            /*unspecified*/ mem_fn(R (T::*)(Args...) const &);
        template<class R, class T, class... Args>
            /*unspecified*/ mem_fn(R (T::*)(Args...) volatile &);
        template<class R, class T, class... Args>
            /*unspecified*/ mem_fn(R (T::*)(Args...) const volatile &);
        template<class R, class T, class... Args>
            /*unspecified*/ mem_fn(R (T::*)(Args...) &&);
        template<class R, class T, class... Args>
            /*unspecified*/ mem_fn(R (T::*)(Args...) const &&);
        template<class R, class T, class... Args>
            /*unspecified*/ mem_fn(R (T::*)(Args...) volatile &&);
        template<class R, class T, class... Args>
            /*unspecified*/ mem_fn(R (T::*)(Args...) const volatile &&);
     
        // polymorphic function wrappers:
        class bad_function_call;
        template<class> class function; // undefined
        template<class R, class... ArgTypes> class function<R(ArgTypes...)>;
        template<class R, class... ArgTypes>
            void swap(function<R(ArgTypes...)>&, function<R(ArgTypes...)>&);
        template<class R, class... ArgTypes>
            bool operator==(const function<R(ArgTypes...)>&, nullptr_t);
        template<class R, class... ArgTypes>
            bool operator==(nullptr_t, const function<R(ArgTypes...)>&);
        template<class R, class... ArgTypes>
            bool operator!=(const function<R(ArgTypes...)>&, nullptr_t);
        template<class R, class... ArgTypes>
            bool operator!=(nullptr_t, const function<R(ArgTypes...)>&);
     
        // hash function base template:
        template <class T> struct hash;
     
        // Hash function specializations
        template <> struct hash<bool>;
        template <> struct hash<char>;
        template <> struct hash<signed char>;
        template <> struct hash<unsigned char>;
        template <> struct hash<char16_t>;
        template <> struct hash<char32_t>;
        template <> struct hash<wchar_t>;
        template <> struct hash<short>;
        template <> struct hash<unsigned short>;
        template <> struct hash<int>;
        template <> struct hash<unsigned int>;
        template <> struct hash<long>;
        template <> struct hash<long long>;
        template <> struct hash<unsigned long>;
        template <> struct hash<unsigned long long>;
        template <> struct hash<float>;
        template <> struct hash<double>;
        template <> struct hash<long double>;
        template<class T> struct hash<T*>;
     
    }
  • template <class T> class reference_wrapper {
    public :
        // types
        typedef T type;
        typedef /*Return type of T*/     result_type;          // not always defined
        typedef /*Single argument of T*/ argument_type;        // not always defined
        typedef /*1st argument of T*/    first_argument_type;  // not always defined
        typedef /*2nd argument of T*/    second_argument_type; // not always defined
     
        // construct/copy/destroy
        reference_wrapper(T&) noexcept;
        reference_wrapper(T&&) = delete;
     
        // do not bind to temporary objects
        reference_wrapper(const reference_wrapper<T>& x) noexcept;
     
        // assignment
        reference_wrapper& operator=(const reference_wrapper<T>& x) noexcept;
     
        // access
        operator T& () const noexcept;
        T& get() const noexcept;
     
        // invocation
        template <class... ArgTypes>
            typename result_of<T&(ArgTypes&&...)>::type
            operator() (ArgTypes&&...) const;
     
    };
  • template<class T> struct is_bind_expression
        : integral_constant<bool, false>
    template<class T> struct is_bind_expression
        : integral_constant<bool, true>
  • class bad_function_call : public std::exception {
    public:
        // constructor:
        bad_function_call() noexcept;
    };
  • template<class> class function; // undefined
     
    template<class R, class... ArgTypes>
    class function<R(ArgTypes...)> {
    public:
        typedef R result_type;      
        typedef T1 argument_type;       //  iff sizeof...(ArgTypes) == 1 and
                                        //  the type in ArgTypes is T1
        typedef T1 first_argument_type; //  iff sizeof...(ArgTypes) == 2 and
                                        //  ArgTypes contains T1 and T2
        typedef T2 second_argument_type;//  iff sizeof...(ArgTypes) == 2 and
                                        //  ArgTypes contains T1 and T2
     
        // construct/copy/destroy:
        function() noexcept;
        function(nullptr_t) noexcept;
        function(const function&);
        function(function&&);
     
        template<class F> function(F);
        template<class A> function(allocator_arg_t, const A&) noexcept;
        template<class A> function(allocator_arg_t, const A&, nullptr_t) noexcept;
        template<class A> function(allocator_arg_t, const A&, const function&);
        template<class A> function(allocator_arg_t, const A&, function&&);
        template<class F, class A> function(allocator_arg_t, const A&, F);
     
        function& operator=(const function&);
        function& operator=(function&&);
        function& operator=(nullptr_t);
        template<class F> function& operator=(F&&);
        template<class F> function& operator=(reference_wrapper<F>) noexcept;
     
        ~function();
     
        // function modifiers:
        void swap(function&) noexcept;
        template<class F, class A> void assign(F&&, const A&);
     
        // function capacity:
        explicit operator bool() const noexcept;
     
        // function invocation:
        R operator()(ArgTypes...) const;
     
        // function target access:
        const std::type_info& target_type() const noexcept;
        template <typename T>       T* target() noexcept;
        template <typename T> const T* target() const noexcept;
     
    };