天天看點

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++

繼續閱讀