模板学习实践三 functor
#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的更多相关文章
- 模板学习实践二 pointer
c++ template学习记录 使用模板将实际类型的指针进行封装 当变量退出作用域 自动delete // 1111.cpp : 定义控制台应用程序的入口点. // #include "s ...
- 模板学习实践一 accumulationtraits
// 11111.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" #include <iostream> #include &l ...
- Flask 学习(三)模板
Flask 学习(三)模板 Flask 为你配置 Jinja2 模板引擎.使用 render_template() 方法可以渲染模板,只需提供模板名称和需要作为参数传递给模板的变量就可简单执行. 至于 ...
- 第04项目:淘淘商城(SpringMvc+Spring+Mybatis) 的学习实践总结【第三天】
淘淘商城(SpringMVC+Spring+Mybatis) 是传智播客在2015年9月份录制的,几年过去了.由于视频里课上老师敲的代码和项目笔记有些细节上存在出入,只有根据日志报错信息作出适当的调 ...
- Appium学习实践(三)测试用例脚本以及测试报告输出
之前Appium学习实践(二)Python简单脚本以及元素的属性设置中的脚本,会有一个问题,就是在每个测试用例完成之后都会执行tearDown,然后重新setUp,这样导致脚本的执行效率偏低,而且会有 ...
- abp学习(三)——文档翻译一
地址:https://aspnetboilerplate.com/Pages/Documents 什么是ASP.NET样板?ASP.NET Boilerplate(ABP)是一个开放源代码且文档齐全的 ...
- 第04项目:淘淘商城(SpringMVC+Spring+Mybatis) 的学习实践总结【第四天】
https://pan.baidu.com/s/1bptYGAb#list/path=%2F&parentPath=%2Fsharelink389619878-229862621083040 ...
- Nagios学习实践系列——基本安装篇
开篇介绍 最近由于工作需要,学习研究了一下Nagios的安装.配置.使用,关于Nagios的介绍,可以参考我上篇随笔Nagios学习实践系列——产品介绍篇 实验环境 操作系统:Red Hat Ente ...
- Nagios学习实践系列——配置研究[监控当前服务器]
其实上篇Nagios学习实践系列——基本安装篇只是安装了Nagios基本组件,虽然能够打开主页,但是如果不配置相关配置文件文件,那么左边菜单很多页面都打不开,相当于只是一个空壳子.接下来,我们来学习研 ...
随机推荐
- 用git,clone依赖的库
git clone https://github.com/influxdata/influxdb-java.git cd crfasrnn git submodule update --init -- ...
- Windows配置ffmpeg
一.ffmpeg简介 ffmpeg是一套可以用来记录.转换数字音频.视频,并能将其转化为流的开源计算机程序.采用LGPL或GPL许可证.它提供了录制.转换以及流化音视频的完整解决方案. 支持操作系统: ...
- 20145319 《网络渗透》MS08_067安全漏洞
20145319 <网络渗透>MS08_067安全漏洞 一 实验内容 了解掌握metasploit平台的一些基本操作,能学会利用已知信息完成简单的渗透操作 了解漏洞MS08_067的相关知 ...
- TypeScript type 类型别名
//6,类型别名 /**类型别名不能出现在声明右侧的任何地方. * 接口 vs. 类型别名 * 另一个重要区别是类型别名不能被extends和implements(自己也不能extends和imple ...
- Northwind学习笔记
一.单表查询 --1.查询订购日期在1996年7月1日至1996年7月15日之间的订单的订购日期.订单ID.客户ID和雇员ID等字段的值 SELECT OrderID , CustomerID , E ...
- C# 调用Tesseract实现OCR
介绍 Tesseract是一个基于Apache2.0协议开源的跨平台ocr引擎,支持多种语言的识别,在Windows和Linux上都有良好的支持. 创建工程 创建一个C#的控制台工程 添加System ...
- centos7安装elasticsearch6.3.x集群并破解安装x-pack
一.环境信息及安装前准备 主机角色(内存不要小于1G): 软件及版本(百度网盘链接地址和密码:链接: https://pan.baidu.com/s/17bYc8MRw54GWCQCXR6pKjg 提 ...
- #161: 给定n*n由0和1组成的矩阵,如果矩阵的每一行和每一列的1的数量都是偶数,则认为符合条件。 你的任务就是检测矩阵是否符合条件
试题描述 给定n*n由0和1组成的矩阵,如果矩阵的每一行和每一列的1的数量都是偶数,则认为符合条件. 你的任务就是检测矩阵是否符合条件,或者在仅改变一个矩阵元素的情况下能否符合条件. "改变 ...
- 富文本编辑器Ueditor 及 hibernate 逆向工程
1.1 富文本编辑器Ueditor ueditor下载地址: http://ueditor.baidu.com/ 下载1.4.3 –utf8-Jsp版本.完整demo可参考下载文件 ...
- .NET Entity Framework基本使用方法
生成模型 EF有两种查询方式,Linq查询 .Lambda表达式 //普通查询 Linq 方式 IQueryable<Book> list = from b in db.Set<Bo ...