模板学习实践三 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基本组件,虽然能够打开主页,但是如果不配置相关配置文件文件,那么左边菜单很多页面都打不开,相当于只是一个空壳子.接下来,我们来学习研 ...
随机推荐
- Python【每日一问】02
问:列表 test = [1,2,3,1,3,4,5,67,7,8,54,1,2,3,4,5,6],如何删除该列表的重复元素? 方法1:利用集合的不重复性 # 利用集合的不重复性 test = [1, ...
- MongoDB复制集技术
复制集搭建 没毛病: https://www.cnblogs.com/nicolegxt/p/6841442.html?utm_source=itdadao&utm_medium=referr ...
- 变式配置简介 VARIANT CONFIGURATION
变式物料类型KMAT; ITEM CATEGORY GROUP: main item 0002 sub item 0004 strategy group:25 requirement type: ke ...
- tfs项目管理
同一个地址下有多个项目,但同一个文件只能映射一次.有两种方式: 1.只添加一次映射,即只给根目录添加映射,如下图,这样西面的具体的项目就不需要挨个添加了. . 2.每个项目挨个添加映射,使用这种方式要 ...
- MPP数据库
MPP数据库 版权声明:本文为博主原创文章,转载请注明出处. https://blog.csdn.net/lyc417356935/article/details/45033069 MPP数据库定 ...
- libevent安装后缺少libevent_openssl.so
最近要使用阿里的rocketmq,需要依赖libevent,所以下了个源码自己编译安装,安装过程按照readme来的: 1 ./configure 2 make 3 make install 但是安装 ...
- C#使用GET、POST请求获取结果,这里以一个简单的用户登陆为例。
1. 使用GET请求获取结果 1.1 创建LoginHandler.aspx处理页面 protected void Page_Load(object sender, EventArgs e) { st ...
- leetcode11
public class Solution { //public int MaxArea(int[] height) //{ // var max = 0; // for (int i = 0; i ...
- leetcode142
public class Solution { public ListNode detectCycle( ListNode head ) { if( head == null || head.next ...
- leetcode438
public class Solution {;public IList<int> FindAnagrams(string s, string p) { List<int> l ...