C++11中的function和bind

     在C++11中添加了两个函数绑定模板, 即function和bind。function模板类和bind模板函数,使用它们可以实现类似函数指针的功能,但却比函数指针更加灵活,特别是函数指向类的非静态成员函数时。

         std::function和std::bind都可以绑定到普通函数(包括类的静态函数)、类的成员函数 。

下面给出简单的示例 :

//
//  main.cpp
//  Cpp11StdBind
//
//  Created by mrsimple on 4/12/14.
//  Copyright (c) 2014 mrsimple. All rights reserved.
//

#include <iostream>
#include <string>
#include <functional>
using namespace std;

// 用于普通函数和类的静态成员函数
typedef function<void (int)> mdPoint;

// 普通函数
void printNum(int num)
{
    cout<<__func__<<", the num is "<<num<<endl;
}


// class
class ThreadPool
{
public:
    ThreadPool() { cout << "max size : " << max_size<<endl; }
    ~ThreadPool() {}
    // 静态函数
    static void setThreadPoolMaxSize(int size) {
        max_size = size ;
        cout<<__func__<<", set thread pool size , " <<size<<endl;
    }

    
    // 非静态成员函数
    void setCoreThread(int core) {
        core_size = core ;
        cout<<__func__<<", core size , " <<core<<endl;

    }

    void outputCoreSize()
    {
        cout<<__func__<<", core size "<<core_size<<endl;
    }
    
    // 使用bind来绑定回调函数
    void bindCallback()
    {
        mdPoint mp = bind(&ThreadPool::setCoreThread, this, placeholders::_1) ;
        mp(10);
    }
public:
   static int max_size;
    // 非静态成员变量, 可以直接初始化数据
    int core_size = 5;
} ;

// 定义静态成员变量
int ThreadPool::max_size = 100 ;


// main
int main(int argc, const char * argv[])
{
    cout<<ThreadPool::max_size<<endl;
    
    // 使用function来绑定函数指针
    mdPoint mp = (&printNum);
    mp(123);
    
    // 指向类的静态函数
    mp = (&ThreadPool::setThreadPoolMaxSize);
    mp(456);
    
    auto tp = new ThreadPool();
    tp->bindCallback() ;
    
    // 带参数
    auto cb2 = bind(&ThreadPool::setCoreThread, tp, placeholders::_1) ;
    cb2(66);

    // 使用bind来设置回调函数, 不带参数的
    auto callback = bind(&ThreadPool::outputCoreSize, tp) ;
    callback();
    
    // lambda的bind
    auto lmd = bind([] { cout<<"this is lambda."<<endl;}) ;
    lmd();
    
    // 传递参数
    auto lmdp = bind([](string name) { cout <<name<<endl; }, placeholders::_1) ;
    lmdp("mr.simple");
    
    // 在placeholders位置上设置参数
    auto lmdp2 = bind([](string name) { cout << "new name " <<name<<endl; } , "MR.SIMPLE -- NEW .");
    lmdp2();
    return 0;
}

function的回调类的成员函数:

#include <iostream>
#include <functional>

using namespace std;



//
class View{

public:
    void onClick(int x, int y)
    {
        cout<<"X : "<<x<<", Y : "<<y<<endl;
    }
} ;

// 定义function类型, 三个参数
function<void (View*, int, int)> clickCallback ;
// 普通函数
function<int (int, int)> f ;


//
int add(int a, int b) {
    return a + b ;
}


//
int main(int argc, const char * argv[])
{

    View button ;
    // 指向成员函数
    clickCallback = &View::onClick ;
    // 进行调用
    clickCallback(&button, 10, 123);
    
    f = add ;
    cout<<"result : "<<f(4,5)<<endl;

    return 0;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值