#include <iostream>
#include <typeinfo>

void foo()
{
std::cout << "foo() called" << std::endl;
}

typedef void FooT(); // FooT is a function type,
// the same type as that of function foo()

int main()
{
foo(); // direct call

// print types of foo and FooT
std::cout << "Types of foo: " << typeid(foo).name()
<< '\n';
std::cout << "Types of FooT: " << typeid(FooT).name()
<< '\n';

FooT* pf = foo; // implicit conversion (decay)
pf(); // indirect call through pointer
(*pf)(); // equivalent to pf()

// print type of pf
std::cout << "Types of pf: " << typeid(pf).name()
<< '\n';

FooT& rf = foo; // no implicit conversion
rf(); // indirect call through reference

// print type of rf
std::cout << "Types of rf: " << typeid(rf).name()
<< '\n';
}

//==========================================================

#include <iostream>

// class for function objects that return constant value
class ConstantIntFunctor {
private:
int value; // value to return on ``function call''
public:
// constructor: initialize value to return
ConstantIntFunctor(int c) : value(c) {
}

// ``function call''
int operator() () const {
return value;
}
};

// client function that uses the function object
void client(ConstantIntFunctor const& cif)
{
std::cout << "calling back functor yields " << cif() << '\n';
}

int main()
{
ConstantIntFunctor seven(7);
ConstantIntFunctor fortytwo(42);
client(seven);
client(fortytwo);
}

//===========================================================

/* The following code example is taken from the book
* "C++ Templates - The Complete Guide"
* by David Vandevoorde and Nicolai M. Josuttis, Addison-Wesley, 2002
*
* (C) Copyright David Vandevoorde and Nicolai M. Josuttis 2002.
* Permission to copy, use, modify, sell and distribute this software
* is granted provided this copyright notice appears in all copies.
* This software is provided "as is" without express or implied
* warranty, and with no claim as to its suitability for any purpose.
*/
#include <vector>
#include <iostream>
#include <cstdlib>

// wrapper for function pointers to function objects
template<int(*FP)()>
class FunctionReturningIntWrapper {
public:
int operator() () {
return FP();
}
};

// example function to wrap
int random_int()
{
return std::rand(); // call standard C function
}

// client that uses function object type as template parameter
template <typename FO>
void initialize(std::vector<int>& coll)
{
FO fo; // create function object
for (std::vector<int>::size_type i = 0; i<coll.size(); ++i) {
coll[i] = fo(); // call function for function object
}
}

int main()
{
// create vector with 10 elements
std::vector<int> v(10);

// (re)initialize values with wrapped function
initialize<FunctionReturningIntWrapper<random_int> >(v);

// output elements
for (std::vector<int>::size_type i = 0; i<v.size(); ++i) {
std::cout << "coll[" << i << "]: " << v[i] << std::endl;
}
}

模板学习实践三 functor的更多相关文章

  1. 模板学习实践二 pointer

    c++ template学习记录 使用模板将实际类型的指针进行封装 当变量退出作用域 自动delete // 1111.cpp : 定义控制台应用程序的入口点. // #include "s ...

  2. 模板学习实践一 accumulationtraits

    // 11111.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" #include <iostream> #include &l ...

  3. Flask 学习(三)模板

    Flask 学习(三)模板 Flask 为你配置 Jinja2 模板引擎.使用 render_template() 方法可以渲染模板,只需提供模板名称和需要作为参数传递给模板的变量就可简单执行. 至于 ...

  4. 第04项目:淘淘商城(SpringMvc+Spring+Mybatis) 的学习实践总结【第三天】

    淘淘商城(SpringMVC+Spring+Mybatis)  是传智播客在2015年9月份录制的,几年过去了.由于视频里课上老师敲的代码和项目笔记有些细节上存在出入,只有根据日志报错信息作出适当的调 ...

  5. Appium学习实践(三)测试用例脚本以及测试报告输出

    之前Appium学习实践(二)Python简单脚本以及元素的属性设置中的脚本,会有一个问题,就是在每个测试用例完成之后都会执行tearDown,然后重新setUp,这样导致脚本的执行效率偏低,而且会有 ...

  6. abp学习(三)——文档翻译一

    地址:https://aspnetboilerplate.com/Pages/Documents 什么是ASP.NET样板?ASP.NET Boilerplate(ABP)是一个开放源代码且文档齐全的 ...

  7. 第04项目:淘淘商城(SpringMVC+Spring+Mybatis) 的学习实践总结【第四天】

    https://pan.baidu.com/s/1bptYGAb#list/path=%2F&parentPath=%2Fsharelink389619878-229862621083040 ...

  8. Nagios学习实践系列——基本安装篇

    开篇介绍 最近由于工作需要,学习研究了一下Nagios的安装.配置.使用,关于Nagios的介绍,可以参考我上篇随笔Nagios学习实践系列——产品介绍篇 实验环境 操作系统:Red Hat Ente ...

  9. Nagios学习实践系列——配置研究[监控当前服务器]

    其实上篇Nagios学习实践系列——基本安装篇只是安装了Nagios基本组件,虽然能够打开主页,但是如果不配置相关配置文件文件,那么左边菜单很多页面都打不开,相当于只是一个空壳子.接下来,我们来学习研 ...

随机推荐

  1. linux 6.5上创建新用户后,不能登陆?

    linux 6.5上创建新用户后,不能登陆? 使用root账户登陆却可以! [root@ log]# useradd mtdk[root@ log]# echo 123abc |passwd --st ...

  2. 2018-2019-2 20175227张雪莹 《Java程序设计》 实验一 Java开发环境的熟悉

    2018-2019-2 20175227张雪莹<Java程序设计> 实验一 Java开发环境的熟悉 一.实验报告封面 课程:Java程序设计 班级:1752班 姓名:张雪莹 学号:2017 ...

  3. pytorch下的lib库 源码阅读笔记(2)

    2017年11月22日00:25:54 对lib下面的TH的大致结构基本上理解了,我阅读pytorch底层代码的目的是为了知道 python层面那个_C模块是个什么东西,底层完全黑箱的话对于理解pyt ...

  4. autofac中文文档

    https://autofaccn.readthedocs.io/zh/latest/index.html

  5. linux怎么样显示命令历史后又显示命令的输入时间

    linux的bash内部命令history就可以显示命令行的命令历史,默认环境执行 history命令后,通常只会显示已执行命令的序号和命令本身.如果想要查看命令历史的时间戳,那么可以执行: 临时显示 ...

  6. 使用jquery+css实现瀑布流布局

    虽然可以直接使用css实现瀑布流布局,但显示的方式有点问题,所以这儿就直接使用jquery+css来实现瀑布流布局,最终效果如下:      思路是通过将每个小块的position设置为relativ ...

  7. 【学习】数据处理基础知识(基本功能)【pandas】

    本章介绍pandas的重要功能,只记录一些重点内容 1.重新索引 pandas对象的一个重要方法是reindex,其作用是创建一个适应用新索引的新对象 #重新索引 obj = pd.Series([4 ...

  8. python入门学习1

    实学习每一种语言,都可以找到很快乐的学习方法.有兴趣,有乐趣,才会一直想学.知道print().input().if/else就可以做一个简陋的游戏了. print() # 打印函数,将信息打印出来 ...

  9. 《深入理解JAVA虚拟机》----------第二章 JAVA内存区域与内存溢出异常,笔记(下)

    2. HotSpot虚拟机对象探秘 2.1 对象的创建 虚拟机遇到一条New指令时,首先将去检查这个指令的参数是否能在常量池中定位到一个类的符号引用,并且检查这个符号引用代表的类是否已被加载.解析和初 ...

  10. Deploy, Issue and Transfer Tokens

    [Deploy, Issue and Transfer Tokens] 本例使用 eosio.token 合约来尝试发行Token. 1.签出 eosio.contracts. git clone h ...