std::bind 详解及参数解析

简介: // Bind_std_function.cpp : 定义控制台应用程序的入口点。 // #include "stdafx.h" #include #include #include #include //学习bind的用法 void f(int n1, int n2...
// Bind_std_function.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include <iostream>
#include <functional>
#include <random>
#include <memory>
//学习bind的用法
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 + 100;
}
struct Foo {
    Foo() = default;
    Foo(const Foo & a)
    {
        data = a.data;
        std::cout << "复制构造" << std::endl;
    }
    void print_sum(int n1, int n2)
    {
        std::cout << n1 + n2 << '\n';
    }
    int data = 10;
};

//////////////////////////////////////////////////////////////////////////
//std::bind的不同的placeholders个数证明调用函数体时需要传入的参数量及位置
//std::bind的时候目标的函数的参数的顺序与bind的时候的顺序的一一对应的
//////////////////////////////////////////////////////////////////////////

int _tmain(int argc, _TCHAR* argv[])
{
    int n = 7;
    auto f1 = std::bind(f, std::placeholders::_2, std::placeholders::_1, 43, std::cref(n), n);
    //第一位置 目标函数(f)的第一个参数 是调用时传的第二个参数
    //第二位置 目标函数(f)的第二个参数 是调用时传的第一个参数
    //第三位置 目标函数(f)的第三个参数 是43
    //第四位置 目标函数(f)的第四个参数 是n的按址传递
    //第五位置 目标函数(f)的第五个参数 是n
    n = 10;
    f1(1, 2);//相当于f(2,1,43,10,7);

    using namespace std::placeholders;
    auto f2 = std::bind(f, _3, std::bind(g, _3), _3, 4, 5);
    //第一位置 目标函数f的第一个参数 是调用时传的第三个参数
    //第二位置 目标函数f的第二个参数 是调用时传的g(第三个参数)
    //第三位置 目标函数f的第三个参数 是调用时传的第三个参数
    //第四位置 目标函数f的第四个参数 是4
    //第五位置 目标函数f的第五个参数 是5
    //由此可见,调用时的第一个参数和第二个参数是没有用的。调用时你把第一个或第二个参数传多少都是没有用的
    f2(1000, 2000, 55);//f(55,g(55),55,4,5);

    // common use case: binding a RNG with a distribution
    std::default_random_engine e;
    std::uniform_int_distribution<> d(0, 10);
    std::cout << d(e) << std::endl;//生成一个随机数

    std::function<int()> rnd = std::bind(d, e);//rnd就相当于d(e)
    for (int n = 0; n < 10; ++n)
        std::cout << rnd() << ' ';
    std::cout << '\n';


    //绑定类成员函数用对象的指针
    Foo foo;
    auto f3 = std::bind(&Foo::print_sum, &foo, 95, _1);
    f3(5);

    // 绑定类成员变量
    std::cout << "测试绑定类成员" << std::endl;
    auto f4 = std::bind(&Foo::data, _1);
    std::cout << f4(foo) << '\n';
    //std::cout << f4(&foo) << '\n';//尝试传入类对象指针编译不通过
    std::cout << f4(std::cref(foo)) << '\n';//引用包装传递

    //测试发现vs2013不支持Foo的智能指针做为f4的参数
    system("pause");
    return 0;
}

 

相关文章
|
1月前
|
存储 安全 编译器
【C++ 包装器类 std::function 和 函数适配器 std::bind】 C++11 全面的std::function和std::bind的入门使用教程
【C++ 包装器类 std::function 和 函数适配器 std::bind】 C++11 全面的std::function和std::bind的入门使用教程
32 0
|
3月前
C++11实用技术(二)std::function和bind绑定器
C++11实用技术(二)std::function和bind绑定器
26 0
|
3月前
|
C++
C++11 function、bind、可变参数模板
C++11 function、bind、可变参数模板
27 0
|
8月前
bind、call、apply 区别
bind、call、apply 区别
51 0
|
C++
c++bind函数使用
c++bind函数使用
96 1
7、call、apply、bind方法
7、call、apply、bind方法
70 0
|
存储 前端开发 编译器
【Example】C++ 回调函数及 std::function 与 std::bind
回调函数是做为参数传递的一种函数,在早期C样式编程当中,回调函数必须依赖函数指针来实现。 而后的C++语言当中,又引入了 std::function 与 std::bind 来配合进行回调函数实现。 标准库中有大量函数应用到了回调函数,其中 std::sort 就是一个经典例子。
1261 0
std::bind1st与std::bind2nd的区别
std::bind1st与std::bind2nd的区别
131 0