loki仿函数原理

问题一:已知函数Fun有2个参数,请完成CTestFunctor类,使得CTestFunctor的()实际调用Fun,部分代码如下:

#include <iostream>

using namespace std ;

class CTestCommand
{
public:
 void operator()(int x,double y)
 {
  cout << "CTestCommand的operator" << x << " " << y << endl;
 }
};

void Fun(double x,int y)
{
 cout << "全局函数" << x << " " << y << endl;
}

void main()
{
 CTestCommand cmd ;
 CTestFunctor<CTestCommand,void,int,double> f(cmd);
 f(3,4.5);

CTestFunctor<void (*)(double,int),void,int,double> f2(Fun);
 f2(100,1000);
}

参考答案:
template<typename FNU,typename R,typename P1,typename P2>
class CTestFunctor
{
public:
 CTestFunctor(const FNU& fun):m_fun(fun)
 {
  
 };
 R operator()(P1 p1,P2 p2)
 {
  return m_fun(p1,p2);
 }
protected:
 FNU m_fun;
};
 
问题二,参数类型不定,但不超过5个。
#include <iostream>

using namespace std ;

class CTestCommand
{
public:
 void operator()()
 {
  cout << "无参数" << endl ;
 }
 void operator()(int x )
 {
  cout << "1参数" << endl ;
 }
 int operator()(double x,int y)
 {
  cout << "2参数" << x << " " << y << endl;
  return 2;
 }
 void operator()(char x,char y ,char z )
 {
  cout << "3参数" << x <<" " << y << " " << z << endl ;
 }
 void operator()(short x,short y,short z,short w)
 {
  cout << "4参数" << x <<" " << y << " " << z << " " << w << endl ;
 }
 void operator()(short x,short y,short z,short w,short v)
 {
  cout << "5参数" << x <<" " << y << " " << z << " " << w << " " << v << endl ;
 }
};

class NullType
{
 NullType();//构造函数只有声明,没实现,所以不会被调用
};
 
template<typename FNU,typename R,typename P1=NullType,typename P2=NullType,typename P3=NullType,typename P4=NullType,typename P5=NullType>
class CTestFunctor
{
public:
 CTestFunctor(const FNU& fun):m_fun(fun)
 {
  
 };
 R operator()()
 {
  return m_fun();
 };
 R operator()(P1 p1)
 {
  return m_fun(p1);
 };
 R operator()(P1 p1,P2 p2)
 {
  return m_fun(p1,p2);
 };
 R operator()(P1 p1,P2 p2,P3 p3)
 {
  return m_fun(p1,p2,p3);
 };
 R operator()(P1 p1,P2 p2,P3 p3,P4 p4)
 {
  return m_fun(p1,p2,p3,p4);
 };
 R operator()(P1 p1,P2 p2,P3 p3,P4 p4,P5 p5)
 {
  return m_fun(p1,p2,p3,p4,p5);
 };
protected:
 FNU m_fun;
};

void main()
{
 CTestCommand cmd ;
 CTestFunctor<CTestCommand,void> f0(cmd);
 f0();

CTestFunctor<CTestCommand,void,int> f1(cmd);
 f1(1);

CTestFunctor<CTestCommand,int,double,int> f2(cmd);
 int x = f2(1,2);

CTestFunctor<CTestCommand,void,char,char,char> f3(cmd);
 f3(1,2,3);

CTestFunctor<CTestCommand,void,short,short,short,short> f4(cmd);
 f4(1,2,3,4);

CTestFunctor<CTestCommand,void,short,short,short,short,short> f5(cmd);
 f5(1,2,3,4,5);
}

问题三:
假定有默认值。
CTestCommand改成如下:
class CTestCommand
{
public: 
 void operator()(short x=1,short y=2,short z=3,short w=4,short v=5)
 {
  cout << "5参数" << x <<" " << y << " " << z << " " << w << " " << v << endl ;
 }
};

f2改成如下:
CTestFunctor<CTestCommand,int,double,int> f2(cmd);
 f2(1,2);
运行正常。

问题四:
看看容错性如何?
class CTestCommand
{
public: 
 void operator()(short x,short y)
 {
  cout << "5参数" << x <<" " << y  << endl ;
 }
};
假定少一个参数,我们看会报错吧。
void main()
{
 CTestCommand cmd ;
 CTestFunctor<CTestCommand,void,short> f1(cmd);
 f1(1); 
}
报错。
void main()
{
 CTestCommand cmd ;
 CTestFunctor<CTestCommand,void,short,short> f1(cmd);
 f1(1); 
}
报错
void main()
{
 CTestCommand cmd ;
 CTestFunctor<CTestCommand,void,short> f1(cmd);
 f1(1,2); 
}
报错。
编译器,就可以发现参数不一致的错误。

loki仿函数原理的更多相关文章

  1. Grafana 系列文章(十):为什么应该使用 Loki

    ️URL: https://grafana.com/blog/2020/09/09/all-the-non-technical-advantages-of-loki-reduce-costs-stre ...

  2. C++ 提高编程

    目录 C++ 提高编程 一. 模板 1. 概念 2. 函数模板 2.1 函数模板语法 2.2 注意事项 2.3 普通函数和函数模板的区别 2.4 普通函数和函数模板的调用规则 2.5 模板的局限性 3 ...

  3. c++异常处理第四篇---不使用try catch语句,使用Loki::ScopeGuard

    转载:神奇的Loki::ScopeGuard 2011-07-05 12:52:05 分类: C/C++ 转载:http://blog.csdn.net/fangqu/article/details/ ...

  4. 函数对象与仿函数(function object and functor)

    part 1. 仿函数在STL组件中的关系 如下图: # 仿函数配合算法完成不同的策略变化. # 适配器套接仿函数. part 2. 仿函数介绍 传递给算法的“函数型实参”不一定得是函数,可以是行为类 ...

  5. Loki之Funtion

    阅读Loki中Funtion源码之后的个人理解,该库归纳起来可以说有三层(C++设计新思维列举到2个参数,此处列举到3个参数),要记住C++的模板其实就是C语言高级的宏定义,如果用户没有用到对应的模板 ...

  6. [STL] STL各容器实现原理

    STL共有六大组件1.容器 2.算法 3.迭代器 4.仿函数 6.适配器 STL容器的实现原理 STL来管理数据十分方便,省去了我们自己构建数据结构的时间.其实,STL的实现也是基于我们常见的数据结构 ...

  7. hash_map原理及C++实现

    一.数据结构:hash_map原理  hash_map基于hash table(哈希表).哈希表最大的长处,就是把数据的存储和查找消耗的时间大大减少,差点儿能够看成是常数时间:而代价不过消耗比較多的内 ...

  8. [GeekBand] STL 仿函数入门详解

    本文参考文献::GeekBand课堂内容,授课老师:张文杰 :C++ Primer 11 中文版(第五版) page 37 :网络资料: 叶卡同学的部落格  http://www.leavesite. ...

  9. 评测Loki日志工具

    评测Loki日志工具 目录 评测Loki日志工具 部署Loki 配置grafana 总结: 优势: 劣势: 本文仅对Loki进行简单评测,不涉及原理和细节. 部署Loki Loki是grafana团队 ...

  10. STL——容器(Set & multiset)之 仿函数(函数对象)functor 的用法

    Set/multiset 中元素的存储数据总是会按照从大到小或者从小到大排列,这个是怎么实现的?这就要说 "仿函数" 这个概念了. 仿函数概念 1. 尽管函数指针被广泛用于实现函数 ...

随机推荐

  1. Git入门--九五小庞

    1.初始化 本地仓库   git init   2.本地仓库的配置 注意,每一个本地仓库只会有一个 user, 这是因为这是你自己本地的仓库,所以显然在你这里只会有一个 user. git confi ...

  2. PHP对接2020年东京奥运会奖牌榜API接口

    2020年东京奥运会奖牌榜查询, 该接口5分钟更新一次, 免费请求, 对接简单, 底部带效果图 成本网页以为大家做好, 可用iframe引入到自己网站或直接跳转, 不加任何广告 1. PHP代码部分 ...

  3. Unity动态骨骼

    教程 https://magicasoft.jp/en/mc2_bonespring_startguide/ 下载 https://download.csdn.net/download/u013898 ...

  4. Nginx设置二级域名映射到不同的Tomcat

    一.前言 在之前的博客中,已经安装好了多个tomcat和nginx,本篇博客将介绍如何设置不同的二级域名转发到不同的tomcat上 二 .配置服务器端 我使用的是腾讯云服务器,只需要在云解析中配置相关 ...

  5. 【攻防世界】2017_Dating_in_Singapore

    1. 首先下载附件,解压出来之后得到的是一个pdf文件和一串数字. 2. 仔细一看发现,这串数字被分割成12段,正好对应pdf文件中的日历月份. 3. 再仔细看发现,这串数字如果两个数字为一组进行划分 ...

  6. Boring Game

    参考:https://blog.csdn.net/qq_45458915/article/details/107804348 模拟题 给出 n 张叠在一起的纸,现在将其连续从左向右折叠 k 次,再从上 ...

  7. 支付宝小程序IDE突然极不稳定

    前情 uni-app是我比较喜欢的跨平台框架,它能开发小程序/H5/APP(安卓/iOS),重要的是对前端开发友好,自带的IDE让开发体验也挺棒的,公司项目就是主推uni-app,因公司项目是快抖微支 ...

  8. ChatGPT应用指南

    ChatGPT应用指南 什么是ChatGPT 定义:ChatGPT是一种基于大规模预训练语言模型(Large Language Model,LLM)的聊天机器人,它能够与人类进行自然.流畅.有趣的对话 ...

  9. JavaScript 多人协作的“修罗场”:如何优雅地规避函数重名问题?

    从刀耕火种的全局变量到模块化工业革命,探索前端协作的进化之路 引言 在多人协作的 JavaScript 项目中,你是否经历过这样的场景:明明只添加了一个小功能,却导致整个页面的弹窗不再工作?经过数小时 ...

  10. python中多线程和多进程的区别

    希望在1分钟内完成500架无人机的路径规划任务,而目前A*算法在50架无人机的情况下需要10秒,意味着在不做优化的情况下处理500架无人机将需要大约100秒,超出你的指标要求.提升计算速度是关键.多线 ...