天天看点

C++11新特性运用:auto decltype std::bind lambda 范围for 模板类函数 回调函数等

本文主要讲解C++11新特性运用:

1.主要讲述回调函数:普通函数作为回调,类成员函数回调

2.auto decltype 运用 自动类型及自动类型推导

3.std::bind的运用:绑定值或std::placeholders 成员变量 成员函数 普通函数 lambda

模板函数 嵌套bind 绑定引用 绑定智能指针容器

4.模板函数定义及使用

.pro记得加上 CONFIG += c++11 console

main.cpp

#include <iostream>
#include <functional>
#include <assert.h>
#include<list>
#include<vector>
#include<tr1/memory>
using namespace std;
using namespace std::placeholders;
typedef std::function<void(int,int)> Fun;
class B{
public:
    void call(int a,Fun f)
    {
        f(a,);
    }
};

class Test{
public:
    void callback(int a,int b)
    {
        cout<<a<<"+"<<b<<"="<<a+b<<endl;
    }

    void bind()
    {
        Fun fun=std::bind(&Test::callback,this,_1,_2);  //std::bind调用类成员函数 this指针
        B b;
        b.call(,fun);
    }
    int a_{};   //成员变量

};

double divide(double d1,int n1) //普通函数
{
    assert(n1 != );
    return d1/n1;
}

template<typename T1,typename T2,typename T3>  //std::bind 绑定模板函数
auto add(const T1 &t1,const T2 &t2,const T3 &t3) ->decltype(t1+t2*t3)
{
    return t1+t2*t3;
}

//....................
void print(int n1,int n2,int n3)
{
    cout<<n1<<" "<<n2<<" "<<n3<<endl;
}
auto addby_1=[](int x,int y) ->int {cout<<"addby() called :"<<endl;return x+y;};
//............


//.................普通函数回调...........................
void printWelcome(int len)
{
    printf("welcome -- %d\n", len);
}

void printGoodbye(int len)
{
    printf("byebye-- %d\n", len);
}

void callback(int times, void (* print)(int))
{
    int i;
    for (i = ; i < times; ++i)
    {
        print(i);
    }
    printf("\n welcome or byebye !\n");
}
//..................................

//............std::bind与智能指针容器list vector
struct Temp
{
    Temp(int i=):i_(i) {}
    void print_(){cout<<"Temp i_ is  "<<i_<<endl;}
    int i_;
};
//智能指针容器
list<shared_ptr<Temp>> vs_=
{
    shared_ptr<Temp>(new Temp()),
    shared_ptr<Temp>(new Temp()),
    shared_ptr<Temp>(new Temp()),
};
vector<shared_ptr<Temp>> vs_1=
{
    shared_ptr<Temp>(new Temp()),
    shared_ptr<Temp>(new Temp()),
    shared_ptr<Temp>(new Temp()),
};
//......................................

int main()
{
    auto d_1=bind(divide,_1,_2);//std::bind普通函数
    cout<<"divide Function: "<<d_1(,)<<endl;

    auto lambda_func=[](int x,int y)->int{return x*y;};   //lambda表达式
    auto ret_bind_lambda=bind(lambda_func,_1,);  //std::bind 一部分绑定到值,一部分绑定到std::placeholders
    cout<<"lambda_Func is:  "<<ret_bind_lambda()<<endl;

    auto nested_f_m=bind(print,_1,bind(addby_1,_1,),_2);   //std::bind嵌套使用 并共享std::placeholders
    nested_f_m(,);    //结果1 4 3

    int x_1();//reference_wrapper<T>
    auto bind_ref=bind(print,,std::cref(x_1),x_1);  //绑定引用 第二个参数使用引用x_1
    bind_ref(); //1 10 10
    x_1=;  //x_1变化,对应引用参数变化
    bind_ref();  //1 300 10

    //std::bind与指针指针容器 list vector
   for(auto str:vs_)                                            //list
       bind(&Temp::print_, str)();

    bind(&Temp::print_, vs_1[])();        // 1         vector
    bind(&Temp::print_, vs_1[])();        // 2
    bind(&Temp::print_, vs_1[])();        // 3


    Test test;
    Test &test1=test;  //test1为test对象引用
    test.bind();  //c++类成员函数作为回调函数   结果为4

    auto mfarg1=bind(&Test::callback,_1,_2,_3);
    mfarg1(test,,);  //使用对象本身调用
    mfarg1(&test,,);//使用对象指针调用
    mfarg1(test1,,);//使用对象引用调用


    auto menber=bind(&Test::a_,test);//std::bind 类成员变量 完全左值绑定   成员变量只能用对象或对象引用,指针不行哦
    cout<<"Class Test menber a_ is"<<menber()<<endl;

    auto addby=bind(add<double,int,int>,_1,_2,_3);  //std::placeholders 占位符 template
    cout<<addby(,,);

    //...........................普通函数作为回调函数
    callback(, printWelcome);
    callback(, printGoodbye);

    return ;
}
           
转载请标明出处:https://blog.csdn.net/qq_35173114/article/details/81008476
c++

继续阅读