C++11 std::bind std::function 高级用法 (c++11的新特性)
发布日期:2022-03-03 10:44:06 浏览次数:16 分类:技术文章

本文共 4819 字,大约阅读时间需要 16 分钟。

从最基础的了解,std::bind和std::function

/*  * File:   main.cpp * Author: Vicky.H * Email:  eclipser@163.com */#include 
#include
#include
#include
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;}
/*  * File:   main2.cpp * Author: Vicky.H * Email:  eclipser@163.com */#include 
#include
void sayHello() { std::cout << "Hello world !" << std::endl;}int sum(int i, int j, int k) { return i + j + k;}template
class Func {public: Func(T fun) { if (!fun) { throw "fun nullptr"; } _fun = fun; } template
R Call(A1 a1, A2 a2, A3 a3, A4 a4, A5 a5) { return _fun(a1, a2, a3, a4, a5); } template
R Call(A1 a1, A2 a2, A3 a3, A4 a4) { return _fun(a1, a2, a3, a4); } template
R Call(A1 a1, A2 a2, A3 a3) { return _fun(a1, a2, a3); } template
R Call(A1 a1, A2 a2) { return _fun(a1, a2); } template
R Call(A1 a1) { return _fun(a1); } template
R Call() { return _fun(); } void Call() { _fun(); }private: T _fun;};#include
template
class Fn {public: Fn(std::function
fun) : _fun(fun) { } R operator()(Args... args) { return _fun(args...); }private: std::function
_fun;};/* * 将函数注册到对象中,通过对象直接调用 */int main(void) { Func
sayHelloFunc(sayHello); sayHelloFunc.Call(); Func
sumFunc(sum); std::cout << "sumFunc.Call
(1, 2, 3) : " << sumFunc.Call
(1, 2, 3) << std::endl; std::cout << "\n---------------------------" << std::endl; Fn<> sayHelloFn(sayHello); sayHelloFn(); Fn
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<int>(1, 2, 3) : 6
---------------------------
Hello world !
sumFn(1, 2, 3) : 6
---------------------------

上面的例子非常有趣,使用了2种方案,将一个函数,注册到一个对象/仿函数中,并且通过一个对象/仿函数来直接调用调用。

例子显而易见的,第2种方案更佳简洁,并且对传递参数有明确的判断,当参数类型或数量不正确的时候,编译器将导致失败。
这种方案,可以将类的成员变量直接作为函数的参数使用,或者,如我:
http://blog.csdn.net/eclipser1987/article/details/23926395
这篇文章中,无法直接调用脚本函数类,有了好的解决办法。这个我将随后补充。

#include 
#include
template
class Fns{private: std::list
> _calls;public: virtual ~Fns() { _calls.clear(); } void connect(std::function
fct) { _calls.push_back(fct); } template
void connect(Object* object, void (Object::*method)(Args...)) { _calls.push_back([object,method](Args... args){(*object.*method)(args...);}); } template
void connect(Object* object, void (Object::*method)(Args...) const) { _calls.push_back([object,method](Args... args){(*object.*method)(args...);}); } template
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...); }};
#include 
#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
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)

转载地址:https://blog.csdn.net/yangjie6898862/article/details/44282037 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:摘自windows程序设计第五版
下一篇:设计一个只能在堆上或栈上实例化的类

发表评论

最新留言

路过按个爪印,很不错,赞一个!
[***.219.124.196]2024年04月06日 15时10分16秒