trait与policy模板应用简单示例
trait与policy模板应用简单示例
accumtraits.hpp // 累加算法模板的trait
// 累加算法模板的trait
#ifndef ACCUMTRAITS_HPP
#define ACCUMTRAITS_HPP template <typename T>
class AccumulationTraits; // 只有声明 template <>
class AccumulationTraits<char> // 把具体类型char映射到int,累加后就返回int
{
public:
typedef int AccT; // 统一的类型别名,表示返回类型
static AccT zero() // 关联一个缺省值,是累加时的初始缺省值
{
return ;
}
}; template <>
class AccumulationTraits<short> // 把具体类型short映射到累加后的返回类型int
{
public:
typedef int AccT;
static AccT zero() // 没有直接在类内部定义static变量并提供缺省值,而是使用了函数
{ // 因为类内部只能对整型和枚举类型的static变量进行初始化 // 其他类型的必须类内部声明,在外部进行初始化
return ;
}
}; template <>
class AccumulationTraits<int>
{
public:
typedef long AccT;
static AccT zero()
{
return ;
}
}; template <>
class AccumulationTraits<unsigned int>
{
public:
typedef unsigned long AccT;
static AccT zero()
{
return ;
}
}; template <>
class AccumulationTraits<float>
{
public:
typedef double AccT;
static AccT zero()
{
return ;
}
}; #endif // !ACCUMTRAITS_HPP
policies.hpp // 累加算法模板的policy
// 累加算法模板的policy
#ifndef POLICIES_HPP
#define POLICIES_HPP template <typename T1, typename T2>
class SumPolicy // 累加的策略
{
public:
static void accumulate(T1 & total, T2 const & value)
{
total += value; // 累加
}
}; template <typename T1, typename T2>
class MultPolicy // 累乘的策略
{
public:
static void accumulate(T1 & total, T2 const & value)
{
total *= value; // 累乘
}
}; #endif // !POLICIES_HPP
accum.hpp // 累加算法模板:实现为类模板,用模板参数来传递policy和trait
// 累加算法模板:实现为类模板,用模板参数来传递policy和trait
// 可用一个内联函数模板作为包装器来包装这个类模板实现
#ifndef ACCUM_HPP
#define ACCUM_HPP #include "accumtraits.hpp"
#include "policies.hpp"
#include <iostream> template < typename T, // 这里使用 typename 和 class 没有区别
const int INITVAL = , // INITVAL 是一个 无类型模板参数
template <typename, typename> class Policy = SumPolicy, // 这里的必须使用class不能是typename, 因为 Policy 是类类型, 默认采用SumPolicy策略
typename Traits = AccumulationTraits<T> > // 模板参数Traits代表要使用的trait
class Accum
{
public:
// AccumulationTraits 是一个 standard traits class (标准特性类)
// AccT 嵌套在 AccumulationTraits<T> 内部类型, 而且 T 是一个模版参数
// AccT 是一个 nested dependent type name (嵌套依赖类型名), 必须被 typename 前置
static typename Traits::AccT accum(T const * beg, T const * end)
{
// total 是一个与 AccT 类型所指向的类型相同的局部变量
// zero() 嵌套在 AccumulationTraits<T> 内部函数, 而且 T 是一个模版参数
//typename Traits::AccT total = Traits::zero(); // 获取缺省值, 返回 0 // 存在问题: 当策略为 MultPolicy 会造成结果始终为 0
typename Traits::AccT total = INITVAL; // 把初始值当作【无类型模板参数】传递进来
while (beg != end) // 作累积运算
{
Policy<Traits::AccT, T>::accumulate(total, *beg); // 使用给定的算法策略来进行累积
++beg;
}
return total; // 返回累积起来的值
}
}; //// 用内联的函数模板来包装, 对默认的参数,提供对应的重载函数
template <typename T, const int INITVAL, template <typename, typename> class Policy, typename Traits>
inline typename Traits::AccT accum(T const * beg, T const * end)
{
std::cout << "<typename T, const int INITVAL, template <typename, typename> class Policy, typename Traits> \n\t---> <T, INITVAL, Policy, Traits>" << std::endl; // 标记使用
return Accum<T, INITVAL, Policy, Traits>::accum(beg, end);
} template <typename T, const int INITVAL, template <typename, typename> class Policy>
inline typename AccumulationTraits<T>::AccT accum(T const * beg, T const * end)
{
std::cout << "<typename T, const int INITVAL, template <typename, typename> class Policy> \n\t---> <T, INITVAL, Policy, AccumulationTraits<T>>" << std::endl; // 标记使用
return Accum<T, INITVAL, Policy, AccumulationTraits<T>>::accum(beg, end);
} template <typename T, const int INITVAL>
inline typename AccumulationTraits<T>::AccT accum(T const * beg, T const * end)
{
std::cout << "<typename T, const int INITVAL> \n\t---> <T, INITVAL, MultPolicy, AccumulationTraits<T>>" << std::endl; // 标记使用
return Accum<T, INITVAL, MultPolicy, AccumulationTraits<T>>::accum(beg, end);
} template <typename T>
inline typename AccumulationTraits<T>::AccT accum(T const * beg, T const * end)
{
std::cout << "<typename T> \n\t---> <T, 1, MultPolicy, AccumulationTraits<T>>" << std::endl; // 标记使用
return Accum<T, , MultPolicy, AccumulationTraits<T>>::accum(beg, end);
} template <>
inline typename AccumulationTraits<int>::AccT accum(int const * beg, int const * end)
{
std::cout << "<> \n\t---> <int, 1, MultPolicy, AccumulationTraits<int>>" << std::endl; // 标记使用
return Accum<int, , MultPolicy, AccumulationTraits<int>>::accum(beg, end);
} #endif // !ACCUM_HPP
mytest.cpp // 使用累加算法的客户端测试代码
// 使用累加算法的客户端测试代码
#include "accum.hpp"
#include <iostream> int main()
{
int num[] = {,,,,}; // 整型数组
std::cout << "============= integer array =============" << std::endl;
std::cout << "the total value of the integer values is "
<< accum<int, , MultPolicy, AccumulationTraits<int>>(&num[], &num[]) << std::endl;
std::cout << "the total value of the integer values is "
<< accum<int, , MultPolicy>(&num[], &num[]) << std::endl;
std::cout << "the total value of the integer values is "
<< accum<int, >(&num[], &num[]) << std::endl;
std::cout << "the total value of the integer values is "
<< accum<int>(&num[], &num[]) << std::endl;
std::cout << "the total value of the integer values is "
<< accum<>(&num[], &num[]) << std::endl;
std::cout << "the total value of the integer values is "
<< accum(&num[], &num[]) << std::endl; char name[] = "templates"; // 创建字符值数组
int length = sizeof(name)-;
std::cout << "============= characters array =============" << std::endl;
std::cout << "the total value of the characters in \""
<< name << "\" is " << accum<char, , SumPolicy, AccumulationTraits<char>>(&name[], &name[length]) << std::endl;
std::cout << "the total value of the characters in \""
<< name << "\" is " << accum<char, , SumPolicy>(&name[], &name[length]) << std::endl;
std::cout << "the total value of the characters in \""
<< name << "\" is " << accum<char, >(&name[], &name[length]) << std::endl;
std::cout << "the total value of the characters in \""
<< name << "\" is " << accum<char>(&name[], &name[length]) << std::endl;
std::cout << "the total value of the characters in \""
<< name << "\" is " << accum<>(&name[], &name[length]) << std::endl;
std::cout << "the total value of the characters in \""
<< name << "\" is " << accum(&name[], &name[length]) << std::endl; system("pause");
return ;
}
输出结果:
============= integer array =============
<typename T, const int INITVAL, template <typename, typename> class Policy, typename Traits>
---> <T, INITVAL, Policy, Traits>
the total value of the integer values is 120
<typename T, const int INITVAL, template <typename, typename> class Policy>
---> <T, INITVAL, Policy, AccumulationTraits<T>>
the total value of the integer values is 120
<typename T, const int INITVAL>
---> <T, INITVAL, MultPolicy, AccumulationTraits<T>>
the total value of the integer values is 120
<>
---> <int, 1, MultPolicy, AccumulationTraits<int>>
the total value of the integer values is 120
<>
---> <int, 1, MultPolicy, AccumulationTraits<int>>
the total value of the integer values is 120
<>
---> <int, 1, MultPolicy, AccumulationTraits<int>>
the total value of the integer values is 120
============= characters array =============
<typename T, const int INITVAL, template <typename, typename> class Policy, typename Traits>
---> <T, INITVAL, Policy, Traits>
the total value of the characters in "templates" is 975
<typename T, const int INITVAL, template <typename, typename> class Policy>
---> <T, INITVAL, Policy, AccumulationTraits<T>>
the total value of the characters in "templates" is 975
<typename T, const int INITVAL>
---> <T, INITVAL, MultPolicy, AccumulationTraits<T>>
the total value of the characters in "templates" is 0
<typename T>
---> <T, 1, MultPolicy, AccumulationTraits<T>>
the total value of the characters in "templates" is 465857536
<typename T>
---> <T, 1, MultPolicy, AccumulationTraits<T>>
the total value of the characters in "templates" is 465857536
<typename T>
---> <T, 1, MultPolicy, AccumulationTraits<T>>
the total value of the characters in "templates" is 465857536
请按任意键继续. . .
trait与policy模板应用简单示例的更多相关文章
- trait与policy模板技术
trait与policy模板技术 我们知道,类有属性(即数据)和操作两个方面.同样模板也有自己的属性(特别是模板参数类型的一些具体特征,即trait)和算法策略(policy,即模板内部的操作逻辑). ...
- [DeviceOne开发]-轮播图和多模板的简单示例
一.简介 这个例子是利用Slideview组件实现循环轮播的效果,同时这个slideview作为一个listview的最上面的一行数, 1. listview有2个模板,一个是以slideview为核 ...
- [JavaWeb基础] 020.Velocity 模板引擎简单示例
1.什么是Velocity 一种J2EE的前端模版技术,和JSP,Freemarker差不多,都是用来展示网页内容的.和JSP不同的是velocity只能显示Action中的数据,不能处理数据.不能写 ...
- web 框架的本质及自定义web框架 模板渲染jinja2 mvc 和 mtv框架 Django框架的下载安装 基于Django实现的一个简单示例
Django基础一之web框架的本质 本节目录 一 web框架的本质及自定义web框架 二 模板渲染JinJa2 三 MVC和MTV框架 四 Django的下载安装 五 基于Django实现的一个简单 ...
- C++ template —— trait与policy类(七)
第15章 trait与policy类---------------------------------------------------------------------------------- ...
- spring-servlet.xml简单示例
spring-servlet.xml简单示例 某个项目中的spring-servlet.xml 记下来以后研究用 <!-- springMVC简单配置 --> <?xml versi ...
- SignalR 简单示例
一.什么是 SignalR ASP.NET SignalR is a library for ASP.NET developers that simplifies the process of add ...
- Web API 简单示例
一.RESTful和Web API Representational State Transfer (REST) is a software architecture style consisting ...
- asp.net模板控件示例
原文:asp.net模板控件示例 模板控件允许将控件数据与其表示形式相分离,模板化控件不提供用户界面. 编写它则是为了实现一个命名容器以及包含属性和方法可由宿主页访问的类,MSDN是这样解释的. 下面 ...
随机推荐
- HTML文本
1.HTML元素 2.HTML属性 3.HTML文本格式化 4.HTML样式 1.HTML元素 1.什么是HTML元素 HTML 元素指的是从开始标签(start tag)到结束标签(end tag) ...
- openGPS.cn - 高精度IP定位原理,定位误差说明
[ip定位历史] 关于IP定位,最早是通过运营商实现,每个运营商申请到的ip段,在某个范围内使用. 因此早期只能是国家为单位的基础数据. 对于比较大的国家,就进一步划分,比如,中国某通讯公司(不打广告 ...
- struts2---自定义类型转换器
从servlet我们知道从页面获取到的参数都是string类型,但是struts2中基本的数据类型,它可以自动帮我们转化为其对应的包装类,就像获取到123,可以自动转化为Integer,但是比如201 ...
- 在分布式数据库中CAP原理CAP+BASE
本篇博文的内容均来源于网络,本人只是整理,仅供学习! 一.关系型数据库 关系型数据库遵循ACID规则 事务在英文中是transaction,和现实世界中的交易很类似,它有如下四个特性: 1.A (At ...
- JAVAWEB复习资料-01
CSS中@import和link两种插入样式表方式有什么不同? 1.link属于HTML标签,除了引入css文件之外还能定义RSS等,而@import只能用于加载CSS. 2.link在引用CSS时, ...
- Windows下memcached的安装配置
下载windows 32位或64位 memcached 下载 memcached_dll 1.将第一个包解压放某个盘下面,比如在c:\memcached.2.在终端(也即cmd命令界面)下输入 'c: ...
- ASP.NET Core 2.0 支付宝当面付之扫码支付
前言 自从微软更换了CEO以后,微软的战略方向有了相当大的变化,不再是那么封闭,开源了许多东西,拥抱开源社区,.NET实现跨平台,收购xamarin并免费提供给开发者等等.我本人是很喜欢.net的,并 ...
- vue2组件之select2调用
目前,项目中使用了纯前端的静态项目+RESTFul接口的模式.为了更好的对数据进行操作,前端使用了vue2的mvvm功能,但是由于不是单页面应用,所以,并没有涉及到其它的如vue-route等功能,也 ...
- KM算法新识
看了很多写的好的文章,但是针对代码注释来讲,这篇文章最合适. 如果人生会有很长,愿你的荣耀永不散场--wenr大牛. #include ...
- Opencv怎么读入,显示,保存图像-OpenCV步步精深
怎么读入图像呢? 我们用 img = cv2.imread('图像路径\原图像名称',0) 原图像名称要有后缀 .png , .jpg等等原图像带有的后缀. 这里我们着重说明一下图像路径,这个路径一定 ...