stdbind

std::bind用法


简介

C++11引入的功能,是函数模板。允许开发者将函数对象、函数指针、函数引用、成员函数指针或数据成员指针与特定参数绑定,从而创建一个新的可调用函数对象。

在标头 <functional> 定义
template< class F, class… Args > /* 未指定 */ bind( F&& f, Args&&… args );(1)(C++11 起) (C++20 起为 constexpr)
template< class R, class F, class… Args > /* 未指定 */ bind( F&& f, Args&&… args );(2)(C++11 起) (C++20 起为 constexpr)
#include <iostream>
#include <functional>
#include <memory>
#include <random>

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(int argc, char** argv) {
    using namespace std::placeholders;
    
    int n = 7;
    // std::bind默认采用值传递,即会复制一份。如果想传递引用,则需要使用std::ref()来进行包装
    auto f1 = std::bind(f, _2, 42, _1, std::cref(n), n);
    n = 10;
    f1(1, 2, 10001); // 等价于f(2, 42, 1, 10, 7);
    // 占位符_1 和 _2解释:_1对应的就是第一个参数1, _2对应的就是第二个参数2
    return 0;
}

output:

$ ../bin/stdbind 
1) 实参重排序和按引用传递:2 42 1 10 7

使用lambda达成相同效果

int main(int argc, char** argv) {
    using namespace std::placeholders;
    
    int n = 7;
    auto lambda = [&ncref = n, n](auto a, auto b, auto /*未使用*/){
        f(b, 42, a, ncref, n);
    };
    n = 10;
    lambda(1, 2, 10001); // 等价于f(2, 42, 1, 10, 7);
    return 0;
}

output:

$ ../bin/stdbind
2) 使用 lambda 达成相同效果:2 42 1 10 7

嵌套绑定子表达式共享占位符

int main(int argc, char** argv) {
    using namespace std::placeholders;
    
    auto f2 = std::bind(f, _3, std::bind(g, _3), _3, 4, 5);
   	f2(10, 11, 12);	// 等价于f(12, 12, 12, 4, 5);
    return 0;
}

output:

$ ../bin/stdbind 
3) 嵌套的绑定子表达式共享占位符:12 12 12 4 5

以分布绑定随机数生成器

int main(int argc, char** argv) {
    using namespace std::placeholders;
    std::default_random_engine e;
    std::uniform_int_distribution<> d(0, 100);
    std::function<int()> rnd = std::bind(d, e);
    for (int i = 0; i < 10; ++i) {
    	std::cout << rnd() << ' ';
    }
    std::cout << std::endl;
    return 0;
}

output:

$ ../bin/stdbind 
4) 以分布绑定随机数生成器:0 13 76 46 53 22 4 68 68 94 

绑定成员函数指针

int main(int argc, char** argv) {
    using namespace std::placeholders;
 	Foo foo;
    auto f3 = std::bind(&Foo::print_sum, &foo, 95, _1);
    f3(5);
    
    return 0;
}

output

$ ../bin/stdbind 
5) 绑定成员函数指针:100

绑定成员函数指针 mem_fn

int main(int argc, char** argv) {
    using namespace std::placeholders;
 	Foo foo;
    auto ptr_to_print_sum = std::mem_fn(&Foo::print_sum);
	auto f4 = std::bind(ptr_to_print_sum, &foo, 95, _1);
    f3(5);
    
    return 0;
}

output

$ ../bin/stdbind 
6) 绑定成员函数指针 mem_fn:100

绑定数据成员指针

int main(int argc, char** argv) {
    using namespace std::placeholders;
    Foo foo;
    auto f5 = std::bind(&Foo::data, _1);
    std::cout << f5(foo) << std::endl;
    return 0;
}

output

$ ../bin/stdbind  
7) 绑定数据成员指针:10

绑定数据成员指针 mem_fn

int main(int argc, char** argv) {
    using namespace std::placeholders;
 	Foo foo;
    auto ptr_to_data = std::mem_fn(&Foo::data);
    auto f6 = std::bind(ptr_to_data, _1);
    std::cout << f6(foo) << std::endl;
    
    return 0;
}

output

$ ../bin/stdbind 
8) 绑定数据成员指针 mem_fn:10

使用智能指针调用被引用对象的成员

int main(int argc, char** argv) {
    using namespace std::placeholders;
 	Foo foo;
    auto ptr_to_data = std::mem_fn(&Foo::data);
    auto f6 = std::bind(ptr_to_data, _1);
    std::cout << f6(std::make_shared<Foo>(foo)) << '\n'
            << f6(std::make_unique<Foo>(foo)) << '\n';
    
    return 0;
}

output

$ ../bin/stdbind
9) 使用智能指针调用被引用对象的成员:10
10
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Vacant-Position

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值