当前位置: 首页 > news >正文

c++ std::invoke

测试代码如下:

#include <iostream>
#include <functional>pair<int, int> getMinAndMax(int a, int b) {int min = a < b ? a : b;int max = a > b ? a : b;return make_pair(min, max);
}// 测试invoke函数的用法
void testInvoke() {// 调用普通函数pair<int, int> res = invoke(getMinAndMax, 9527, 1);cout << "min: " << res.first << ", max: " << res.second << endl;// 调用成员函数struct MyStru {string info;void test(int x) { std::cout << "x = " << x << std::endl; }static void staticFunc(string str) {std::cout << str << std::endl;}};MyStru myStruct;myStruct.info = "小甜甜";invoke(&MyStru::test, myStruct, 10);invoke(&MyStru::staticFunc, "牛夫人");// 调用lambda表达式invoke([](string str) { std::cout << str << std::endl; }, "我对你的敬仰如滔滔江水连绵不绝。");// 通过智能指针调用auto ptr = std::make_unique<MyStru>();invoke(&MyStru::test, myStruct, 9527);// 修改成员变量cout << "修改前info: " << myStruct.info << endl;invoke(&MyStru::info, myStruct) = "一个月几百块你玩什么命啊";cout << "修改后info: " << myStruct.info << endl;// 调用仿函数:struct Functor {void operator()(const string& str) const {std::cout << str << std::endl;}};Functor f;std::invoke(f, "段王爷");
}

打印:

ok.

相关文章:

  • Docker Compose完整教程
  • 【Chipyard】 conda 环境安装与使用
  • 黑马python(四)
  • 正则表达式:开启文本处理的魔法之门
  • Git不能更新以及提交代码,提示链接超时,本地凭证无问题
  • Binder
  • ONLYOFFICE 协作空间 企业版使用秘籍-1.如何使用外部存储
  • 达梦数据库部署veri数据对比工具
  • 3.3.2 纠错编码(海明校验码)
  • 板凳-------Mysql cookbook学习 (十--5)
  • 鸿蒙Next仓颉语言开发实战教程:订单列表
  • Spring Cloud与Alibaba微服务架构全解析
  • Android GreenDAO 通过 Key 查询数据库数据慢问题优化
  • 力扣hot100--反转链表
  • 多线程应用
  • 详细解释aruco::markdetection _detectInitialCandidates函数
  • Java八股文——Spring「MyBatis篇」
  • Linux开发工具之VsCode(Filezila、MobaXterm、Vim三合一)
  • git的常用方法
  • 【Unity优化】提高热更新和打包速度
  • 做网站一天能接多少单/洛阳seo网站
  • 汕尾北京网站建设/网站收录提交入口网址
  • 个性化网站建设/5118网站查询
  • 做网站界面多少钱/网络广告营销经典案例
  • 设计图片制作软件免费/惠州抖音seo
  • 郑州网站推广公司排名/求职seo推荐