实现调用任意函数的代码

来源:互联网 发布:大数据的权威期刊 编辑:程序博客网 时间:2024/05/19 00:10
#include <iostream>#include <functional>using namespace std;class A{public:  template<class U,class ...T> //可变模板参数  void cmd(U f,T... args)  {    f(args...);  }  void f1(int a)  {    cout<<"f1"<<endl;  }  void f2(double b,float c)  {    cout<<"f2"<<endl;  }protected:private:};int main(){  using namespace std::placeholders;  A a;  auto bf1 = bind(&A::f1,&a,_1);  auto bf2 = bind(&A::f2,&a,_1,_2);  a.cmd(bf1,3);  a.cmd(bf2,2,1.2);  getchar();  return 0;}
//以上代码需运行在gcc4.6 以上的编译器. vs2013 也可以支持.

编译时加上 

g++  -std=c++0x -Wall


原创粉丝点击