首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

std::bind

Defined in header <functional>

template< class F, class... Args > /*unspecified*/ bind( F&& f, Args&&... args );

(1)

(since C++11)

template< class R, class F, class... Args > /*unspecified*/ bind( F&& f, Args&&... args );

(2)

(since C++11)

函数模板bind生成一个转发调用包装器。f调用此包装器相当于调用f它的一些论点args...

参数

f

-

Callable object (function object, pointer to function, reference to function, pointer to member function, or pointer to data member) that will be bound to some arguments

args

-

list of arguments to bind, with the unbound arguments replaced by the placeholders _1, _2, _3... of namespace std::placeholders

返回值

未指定类型的函数对象T,其中std::is_bind_expression<T>::value==true委员会有下列成员:

STD:绑定返回类型

成员对象

返回类型std::bind持有类型为的成员对象。std::decay<F>::typestd::forward<F>(f),每个对象都有一个args...,类型std::decay<Arg_i>::type,类似于std::forward<Arg_i>(arg_i)...

建设者

返回类型std::bindCopyConstructible如果在%29上面指定的所有成员对象%28都是CopyConstrucable,并且是MoveConstructible否则。该类型定义以下成员:

成员类型result_type

1%29%28在C++17%29中被否决F是指向函数的指针或指向成员函数的指针,result_type的返回类型。F.如果F是一个具有嵌套类型的类类型。result_type,然后result_typeF::result_type.否则不会result_type被定义。

2%29%28在C++17%29中被否决result_type就是R...

成员函数operator()

给定对象g从先前调用到的bind,当它在函数调用表达式中被调用时。g(u1, u2, ... uM),则发生对存储对象的调用,就像std::invoke(fd,std::forward<V1>(v1),std::forward<V2>(v2), ...,std::forward<VN>(vN)),在哪里fd是类型的值。std::decay_t<F>绑定参数的值和类型。v1, v2, ..., vN如下所示。

  • 如果存储的参数arg是类型的std::reference_wrapper<T>%28例如,std::refstd::cref在初始调用中使用bind%29,那么这个论点vnstd::invoke以上呼叫arg.get()和类型Vn在同一个电话里T&存储的参数通过引用传递到被调用的函数对象。
  • 如果存储的参数arg是类型的T对此std::is_bind_expression<T>::value==true%28--例如,另一种bind表达式直接传递到bind%29,那么bind执行函数组合:而不是传递绑定子表达式将返回的函数对象,而是急切地调用子表达式,并将其返回值传递给外部可调用对象。如果绑定子表达式有任何占位符参数,则从u1, u2, ...29%。具体来说,争论vnstd::invoke以上呼叫arg(std::forward<Uj>(uj)...)和类型Vn在同一个电话里std::result_of_t<T cv &(Uj&&...)>&&%28 cv资格与g29%。
  • 如果存储的参数arg是类型的T,其中std::is_placeholder<T>::value!=0%28意指占位符,如std::placeholders::_1, _2, _3, ...的初始调用的参数。bind%29,则占位符%28所指示的参数u1_1,,,u2_2,etc%29传递给可调用对象:参数vnstd::invoke以上呼叫std::forward<Uj>(uj)以及相应的类型Vn在同一个电话里Uj&&...
  • 否则,普通存储的参数arg作为lvalue参数传递给可调用对象:vnstd::invoke上面的呼叫很简单arg以及相应的类型VnT cv &,其中cv的cv资格与g...

调用中提供的一些参数g()中存储的占位符不匹配。g,计算和丢弃未使用的参数。

如果g是易挥发的-合格%28i。e.,其cv-限定符为volatileconst volatile%29,行为未定义。

例外

只有在构造std::decay<F>::typestd::forward<F>(f)的任何构造函数std::decay<Arg_i>::type从相应的std::forward<Arg_i>(arg_i)抛出Arg_i是第一种类型arg_i的第一个论点是Args... args...

注记

如上文所述Callable,当调用指向非静态成员函数的指针或指向非静态数据成员的指针时,第一个参数必须是引用或指针%28,其中可能包括诸如std::shared_ptrstd::unique_ptr%29访问其成员的对象。

要绑定的参数是复制或移动的,除非包装在std::refstd::cref...

相同绑定表达式%28倍数中的重复占位符_1%27s(例如%29)是允许的,但是只有在相应的参数%28中才能很好地定义结果。u1%29是单价或不可移动的rvalue.

二次

代码语言:javascript
复制
#include <random>
#include <iostream>
#include <memory>
#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;  // for _1, _2, _3...
 
    // demonstrates argument reordering and pass-by-reference
    int n = 7;
    // (_1 and _2 are from std::placeholders, and represent future
    // arguments that will be passed to f1)
    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
                    // makes a call to f(2, 1, 42, n, 7)
 
    // 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); // a copy of e is stored in rnd
    for(int n=0; n<10; ++n)
        std::cout << rnd() << ' ';
    std::cout << '\n';
 
    // bind to a pointer to member function
    Foo foo;
    auto f3 = std::bind(&Foo::print_sum, &foo, 95, _1);
    f3(5);
 
    // bind to a pointer to data member
    auto f4 = std::bind(&Foo::data, _1);
    std::cout << f4(foo) << '\n';
 
    // smart pointers can be used to call members of the referenced objects, too
    std::cout << f4(std::make_shared<Foo>(foo)) << '\n'
              << f4(std::make_unique<Foo>(foo)) << '\n';
}

二次

产出:

二次

代码语言:javascript
复制
2 1 42 10 7
12 12 12 4 5
1 5 0 2 0 8 2 2 10 8
100
10
10
10

二次

另见

_1, _2, _3, _4, ... (C++11)

placeholders for the unbound arguments in a std::bind expression (constant)

mem_fn (C++11)

creates a function object out of a pointer to a member (function template)

代码语言:txt
复制
 © cppreference.com

在CreativeCommonsAttribution下授权-ShareAlike未移植许可v3.0。

扫码关注腾讯云开发者

领取腾讯云代金券