#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. ios下表单post使用gzip模式

    使用afnetworking,服务器参考的这里 ios端,使用自己的序列化类 manager.requestSerializer = [MyHttpRequestSerializer new];[ma ...

  2. linux--切换ipython解释器到python3

    Ipython修改为python3解释器: which ipython --得到路径 cat 路径--查看执行的解释器版本 sudo gedit 路径--修改解释器版本为python3 保存即可,保存 ...

  3. Browser Render Engine & Javascript Engine

    Browser Render Engine Programming Language Open Source Javascript Engine Comparation for CSS Compati ...

  4. linux git 保存用户名和密码

    一.通过文件方式 1.在~/下, touch创建文件 .git-credentials, 用vim编辑此文件,输入内容格式: touch .git-credentials vim .git-crede ...

  5. ros有一个比较安全的登录方案:二次登录防火墙

    原文: https://www.winbox.org/ /ip firewall address-list add address=10.0.0.0/8 list=login /ip firewall ...

  6. [UE4]Menu Anchor,菜单锚点

    一.想要弹出某个菜单的时候,Menu Anchor可以做为菜单弹出的位置. 二.Menu Anchor本身不显示任何东西 三.Menu Class:选择要弹出的UI,可以是任意的UserWidget ...

  7. docker入门 什么是docker? 为什么使用docker?

    1.什么是docker? 轻量级操作系统虚拟化解决方案 2.为什么使用docker? 1.docker的启动是秒级的,比传统虚拟机快很多 2.资源利用率高,一台主机上可同时运行数千个docker容器 ...

  8. 《Celeste》 开发者是如何精心制作“冲刺”的

    转自:http://www.gameres.com/804804.html 简介与序曲 在Celesete里,许多细微的行动都是发生在转瞬之间的,甚至往往比你想象中还要“转瞬之间”. 这里是 [游戏机 ...

  9. Python 爬虫 Vimeo视频下载链接

    python vimeo_d.py https://vimeo.com/228013581 在https://vimeo.com/上看到稀罕的视频 按照上面加上视频的观看地址运行即可获得视频下载链接 ...

  10. WOW

    WOW http://bbs.ngacn.cc/read.php?tid=4992959  http://ngasave.us/popcn/?t=gems  地精科技:国服最流行 http://bbs ...