std::bind

来自cppreference.com
< cpp‎ | utility‎ | functional


 
 
 
函数对象
函数包装
(C++11)
(C++11)
(C++17)
绑定
bind
(C++11)
引用包装
(C++11)
(C++11)
运算符包装
取反器
(过时)
(过时)

(过时)
查找器
旧绑定器与适配器
(C++17 前)
(C++17 前)
(C++17 前)
(C++17 前)
(C++17 前)
(C++17 前)
(C++17 前)
(C++17 前)
(C++17 前)
(C++17 前)
(C++17 前)
(C++17 前)

(C++17 前)
(C++17 前)
(C++17 前)
(C++17 前)
(C++17 前)
 
定义于头文件 <functional>
template< class F, class... Args >
/*unspecified*/ bind( F&& f, Args&&... args );
(1) (C++11 起)
template< class R, class F, class... Args >
/*unspecified*/ bind( F&& f, Args&&... args );
(2) (C++11 起)
函数模板bind产生f转发呼叫的包装。调用此包装是相当于调用f与它的参数绑定到args
原文:
The function template bind generates a forwarding call wrapper for f. Calling this wrapper is equivalent to invoking f with some of its arguments bound to args.
文本通过谷歌翻译机器翻译。
你可以帮忙校正和验证翻译。点击此处查看指示。
在内部,包装std::decay<F>::type构造类型std::forward<F>(f)持有对象,和每个args...,类似的结构类型std::decay<Arg_i>::type,从std::forward<Arg_i>(arg_i)每一个对象.
原文:
Internally, the wrapper holds a member object of type std::decay<F>::type constructed from std::forward<F>(f), and one object per each of args..., of type std::decay<Arg_i>::type, similarly constructed from std::forward<Arg_i>(arg_i).
文本通过谷歌翻译机器翻译。
你可以帮忙校正和验证翻译。点击此处查看指示。

目录

[编辑] 参数

f -
将被绑定到的一些参数进行调用对象(函数对象,函数指针,引用函数,成员函数的指针,指针到成员数据)
原文:
invocable object (function object, pointer to function, reference to function, pointer to member function, or pointer to member data) that will be bound to some arguments
文本通过谷歌翻译机器翻译。
你可以帮忙校正和验证翻译。点击此处查看指示。
args -
列表中的参数进行绑定,未绑定的参数替换的占位符_1, _2, _3...
原文:
list of arguments to bind, with the unbound arguments replaced by the placeholders _1, _2, _3...
文本通过谷歌翻译机器翻译。
你可以帮忙校正和验证翻译。点击此处查看指示。

[编辑] 返回值

函数对象的类型不明确T,其中std::is_bind_expression<T>::value == true,并且可以存储在std::function。该对象f和所有args是可移动的,如果是可移动的,否则,是能够复制。该类型定义了以下成员组成:
原文:
A function object of unspecified type T, for which std::is_bind_expression<T>::value == true, and which can be stored in std::function. The object is movable if f and all args are movable, and is copyable otherwise. The type defines the following members:
文本通过谷歌翻译机器翻译。
你可以帮忙校正和验证翻译。点击此处查看指示。

的std :: bind的
原文:
std::bind
文本通过谷歌翻译机器翻译。
你可以帮忙校正和验证翻译。点击此处查看指示。
Return type

Member type result_type

1) If F is a pointer to function or a pointer to member function, result_type is the return type of F. If F is a class type with nested typedef result_type, then result_type is F::result_type. Otherwise no result_type is defined.

2) result_type is exactly R.

Member function operator()

Given an object g obtained from an earlier call to bind, when it is invoked in a function call expression g(u1, u2, ... uM), an invocation of the stored object of type std::decay<F>::type takes place, with arguments defined as follows:

  • If the argument is of type std::reference_wrapper<T> (e.g. std::ref or std::cref) was used in the initial call to bind, then the reference T& stored in the bound argument is passed to the invocable object.
  • If std::is_bind_expression<T>::value == true (i.e. another sbind subexpression was used as an argument in the initial call to bind), then that bind-subexpressin is invoked immediately and its result is passed to the invocable object. If the bind subexpression has any placeholder arguments, they are picked from u1, u2, ....
  • If std::is_placeholder<T>::value != 0 (i.e., _1, _2, _3, ... was used as the argument to the initial call to bind), then the argument indicated by the placeholder (u1 for _1, u2 for _2, etc) is passed to the invocable object as std::forward<Uj>(uj).
  • Otherwise, the stored argument is passed to the invocable object as-is.

If some of the arguments that are supplied in the call to g() are not matched by any placeholders stored in g, the unused arguments are evaluated and discarded.

[编辑] 例外

,只抛出,如果建设std::decay<F>::typestd::forward<F>(f)抛出,或从corrspondingstd::decay<Arg_i>::typestd::forward<Arg_i>(arg_i)的构造函数抛出Arg_i是第i个类型,arg_i是第i个参数的Args... args.
原文:
Only throws if construction of std::decay<F>::type from std::forward<F>(f) throws, or any of the constructors for std::decay<Arg_i>::type from the corrsponding std::forward<Arg_i>(arg_i) throws where Arg_i is the ith type and arg_i is the ith argument in Args... args.
文本通过谷歌翻译机器翻译。
你可以帮忙校正和验证翻译。点击此处查看指示。

[编辑] 注释

要绑定的参数被复制或移动的,并且永远不会通过引用传递,除非包裹在std::refstd::cref.
原文:
The arguments to bind are copied or moved, and are never passed by reference unless wrapped in std::ref or std::cref.
文本通过谷歌翻译机器翻译。
你可以帮忙校正和验证翻译。点击此处查看指示。
重复在相同的绑定表达式的占位符(多_1的例子)是允许的,但结果却只有明确定义,如果相应的参数(u1)是一个左值或不可移动的右值.
原文:
Duplicate placeholders in the same bind expression (multiple _1's for example) are allowed, but the results are only well defined if the corresponding argument (u1) is an lvalue or non-movable rvalue.
文本通过谷歌翻译机器翻译。
你可以帮忙校正和验证翻译。点击此处查看指示。

[编辑] 示例

#include <random>
#include <iostream>
#include <functional>
 
void f(int n1, int n2, int n3, const int& n4, int n5)
{
    std::cout << n1 << ' ' << n2 << ' ' << n3 << ' ' << n4 << ' ' << n5 << '\n';
}
 
int g(int n1)
{
    return n1;
}
 
struct Foo {
    void print_sum(int n1, int n2)
    {
        std::cout << n1+n2 << '\n';
    }
    int data = 10;
};
 
int main()
{
    using namespace std::placeholders;
 
    // demonstrates argument reordering and pass-by-reference
    int n = 7;
    auto f1 = std::bind(f, _2, _1, 42, std::cref(n), n);
    n = 10;
    f1(1, 2, 1001); // 1 is bound by _1, 2 is bound by _2, 1001 is unused
 
    // nested bind subexpressions share the placeholders
    auto f2 = std::bind(f, _3, std::bind(g, _3), _3, 4, 5);
    f2(10, 11, 12);
 
    // common use case: binding a RNG with a distribution
    std::default_random_engine e;
    std::uniform_int_distribution<> d(0, 10);
    std::function<int()> rnd = std::bind(d, e);
    for(int n=0; n<10; ++n)
        std::cout << rnd() << ' ';
    std::cout << '\n';
 
    // bind to a member function
    Foo foo;
    auto f3 = std::bind(&Foo::print_sum, foo, 95, _1);
    f3(5);
 
    // bind to member data
    auto f4 = std::bind(&Foo::data, _1);
    std::cout << f4(foo) << '\n';
}

输出:

2 1 42 10 7
12 12 12 4 5
1 5 0 2 0 8 2 2 10 8
100
10

[编辑] 另请参阅

std::bind表达式中用于非绑定的参数的占位符
(常量) [编辑]
(C++11)
从指向成员的指针创建函数对象
原文:
creates a function object out of a pointer to a member
文本通过谷歌翻译机器翻译。
你可以帮忙校正和验证翻译。点击此处查看指示。

(函数模板) [编辑]