從最基礎的瞭解,std::bind和std::function
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
/* * File: main.cpp * Author: Vicky.H * Email: eclipser@163.com */ #include <iostream> #include <functional> #include <typeinfo> #include <string.h> int add1(int i, int j, int k) { return i + j + k; } class Utils { public: Utils(const char* name) { strcpy(_name, name); } void sayHello(const char* name) const { std::cout << _name << " say: hello " << name << std::endl; } static int getId() { return 10001; } int operator()(int i, int j, int k) const { return i + j + k; } private: char _name[32]; }; /* * */ int main(void) { // 繫結全域性函式 auto add2 = std::bind(add1, std::placeholders::_1, std::placeholders::_2, 10); // 函式add2 = 繫結add1函式,引數1不變,引數2不變,引數3固定為10. std::cout << typeid(add2).name() << std::endl; std::cout << "add2(1,2) = " << add2(1, 2) << std::endl; std::cout << "\n---------------------------" << std::endl; // 繫結成員函式 Utils utils("Vicky"); auto sayHello = std::bind(&Utils::sayHello, utils/*呼叫者*/, std::placeholders::_1/*引數1*/); sayHello("Jack"); auto sayHelloToLucy = std::bind(&Utils::sayHello, utils/*呼叫者*/, "Lucy"/*固定引數1*/); sayHelloToLucy(); // 繫結靜態成員函式 auto getId = std::bind(&Utils::getId); std::cout << getId() << std::endl; std::cout << "\n---------------------------" << std::endl; // 繫結operator函式 auto add100 = std::bind(&Utils::operator (), utils, std::placeholders::_1, std::placeholders::_2, 100); std::cout << "add100(1, 2) = " << add100(1, 2) << std::endl; // 注意:無法使用std::bind()繫結一個過載函式 return 0; } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 |
/* * File: main2.cpp * Author: Vicky.H * Email: eclipser@163.com */ #include <iostream> #include <typeinfo> void sayHello() { std::cout << "Hello world !" << std::endl; } int sum(int i, int j, int k) { return i + j + k; } template <typename T> class Func { public: Func(T fun) { if (!fun) { throw "fun nullptr"; } _fun = fun; } template<typename R, typename A1, typename A2, typename A3, typename A4, typename A5> R Call(A1 a1, A2 a2, A3 a3, A4 a4, A5 a5) { return _fun(a1, a2, a3, a4, a5); } template<typename R, typename A1, typename A2, typename A3, typename A4> R Call(A1 a1, A2 a2, A3 a3, A4 a4) { return _fun(a1, a2, a3, a4); } template<typename R, typename A1, typename A2, typename A3> R Call(A1 a1, A2 a2, A3 a3) { return _fun(a1, a2, a3); } template<typename R, typename A1, typename A2> R Call(A1 a1, A2 a2) { return _fun(a1, a2); } template<typename R, typename A1> R Call(A1 a1) { return _fun(a1); } template<typename R> R Call() { return _fun(); } void Call() { _fun(); } private: T _fun; }; #include <functional> template<typename R = void, typename... Args> class Fn { public: Fn(std::function<R(Args...)> fun) : _fun(fun) { } R operator()(Args... args) { return _fun(args...); } private: std::function<R(Args...) > _fun; }; /* * 將函式註冊到物件中,通過物件直接呼叫 */ int main(void) { Func<void(*)() > sayHelloFunc(sayHello); sayHelloFunc.Call(); Func<int (*)(int, int, int) > sumFunc(sum); std::cout << "sumFunc.Call<int>(1, 2, 3) : " << sumFunc.Call<int>(1, 2, 3) << std::endl; std::cout << "\n---------------------------" << std::endl; Fn<> sayHelloFn(sayHello); sayHelloFn(); Fn<int, int, int, int> sumFn(sum); std::cout << "sumFn(1, 2, 3) : " << sumFn(1, 2, 3) << std::endl; std::cout << "\n---------------------------" << std::endl; return 0; } |
Hello world !
sumFunc.Call(1, 2, 3) : 6
—————————
Hello world !
sumFn(1, 2, 3) : 6
—————————
上面的例子非常有趣,使用了2種方案,將一個函式,註冊到一個物件/仿函式中,並且通過一個物件/仿函式來直接呼叫呼叫。
例子顯而易見的,第2種方案更佳簡潔,並且對傳遞引數有明確的判斷,當引數型別或數量不正確的時候,編譯器將導致失敗。
這種方案,可以將類的成員變數直接作為函式的引數使用,或者,如我:
http://blog.csdn.net/eclipser1987/article/details/23926395
這篇文章中,無法直接呼叫指令碼函式類,有了好的解決辦法。這個我將隨後補充。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
#include <list> #include <functional> template<typename... Args> class Fns { private: std::list<std::function<void(Args...)> > _calls; public: virtual ~Fns() { _calls.clear(); } void connect(std::function<void(Args...)> fct) { _calls.push_back(fct); } template<typename Object> void connect(Object* object, void (Object::*method)(Args...)) { _calls.push_back([object,method](Args... args){(*object.*method)(args...);}); } template<typename Object> void connect(Object* object, void (Object::*method)(Args...) const) { _calls.push_back([object,method](Args... args){(*object.*method)(args...);}); } template<typename Object> void connect(const Object* object, void (Object::*method)(Args...) const) { _calls.push_back([object,method](Args... args){(*object.*method)(args...);}); } void emit(Args... args) { for(auto call : _calls) call(args...); } }; |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
#include <cstdio> #include "Signal.hpp" class Foo { public: void bar(int x, int y) { printf("Foo::bar(%d, %d)\n", x, y); } }; void foobar(int x, int y) { printf("foobar(%d, %d)\n", x, y); } int main(void) { Foo foo; Fns<int, int> s; // Connect a function s.connect(foobar); // Connect a class method s.connect(&foo, &Foo::bar); // Create and connect some lambda expression s.connect([&foo](int x, int y){ printf("lambda::"); foo.bar(x, y); }); // Emit the signal ! s.emit(4, 2); getchar(); return 0; } |
foobar(4, 2)
Foo::bar(4, 2)
lambda::Foo::bar(4, 2)