trait与policy模板应用简单示例

accumtraits.hpp // 累加算法模板的trait

  1. // 累加算法模板的trait
  2. #ifndef ACCUMTRAITS_HPP
  3. #define ACCUMTRAITS_HPP
  4.  
  5. template <typename T>
  6. class AccumulationTraits; // 只有声明
  7.  
  8. template <>
  9. class AccumulationTraits<char> // 把具体类型char映射到int,累加后就返回int
  10. {
  11. public:
  12. typedef int AccT; // 统一的类型别名,表示返回类型
  13. static AccT zero() // 关联一个缺省值,是累加时的初始缺省值
  14. {
  15. return ;
  16. }
  17. };
  18.  
  19. template <>
  20. class AccumulationTraits<short> // 把具体类型short映射到累加后的返回类型int
  21. {
  22. public:
  23. typedef int AccT;
  24. static AccT zero() // 没有直接在类内部定义static变量并提供缺省值,而是使用了函数
  25. { // 因为类内部只能对整型和枚举类型的static变量进行初始化 // 其他类型的必须类内部声明,在外部进行初始化
  26. return ;
  27. }
  28. };
  29.  
  30. template <>
  31. class AccumulationTraits<int>
  32. {
  33. public:
  34. typedef long AccT;
  35. static AccT zero()
  36. {
  37. return ;
  38. }
  39. };
  40.  
  41. template <>
  42. class AccumulationTraits<unsigned int>
  43. {
  44. public:
  45. typedef unsigned long AccT;
  46. static AccT zero()
  47. {
  48. return ;
  49. }
  50. };
  51.  
  52. template <>
  53. class AccumulationTraits<float>
  54. {
  55. public:
  56. typedef double AccT;
  57. static AccT zero()
  58. {
  59. return ;
  60. }
  61. };
  62.  
  63. #endif // !ACCUMTRAITS_HPP

policies.hpp // 累加算法模板的policy

  1. // 累加算法模板的policy
  2. #ifndef POLICIES_HPP
  3. #define POLICIES_HPP
  4.  
  5. template <typename T1, typename T2>
  6. class SumPolicy // 累加的策略
  7. {
  8. public:
  9. static void accumulate(T1 & total, T2 const & value)
  10. {
  11. total += value; // 累加
  12. }
  13. };
  14.  
  15. template <typename T1, typename T2>
  16. class MultPolicy // 累乘的策略
  17. {
  18. public:
  19. static void accumulate(T1 & total, T2 const & value)
  20. {
  21. total *= value; // 累乘
  22. }
  23. };
  24.  
  25. #endif // !POLICIES_HPP

accum.hpp // 累加算法模板:实现为类模板,用模板参数来传递policy和trait

  1. // 累加算法模板:实现为类模板,用模板参数来传递policy和trait
  2. // 可用一个内联函数模板作为包装器来包装这个类模板实现
  3. #ifndef ACCUM_HPP
  4. #define ACCUM_HPP
  5.  
  6. #include "accumtraits.hpp"
  7. #include "policies.hpp"
  8. #include <iostream>
  9.  
  10. template < typename T, // 这里使用 typename 和 class 没有区别
  11. const int INITVAL = , // INITVAL 是一个 无类型模板参数
  12. template <typename, typename> class Policy = SumPolicy, // 这里的必须使用class不能是typename, 因为 Policy 是类类型, 默认采用SumPolicy策略
  13. typename Traits = AccumulationTraits<T> > // 模板参数Traits代表要使用的trait
  14. class Accum
  15. {
  16. public:
  17. // AccumulationTraits 是一个 standard traits class (标准特性类)
  18. // AccT 嵌套在 AccumulationTraits<T> 内部类型, 而且 T 是一个模版参数
  19. // AccT 是一个 nested dependent type name (嵌套依赖类型名), 必须被 typename 前置
  20. static typename Traits::AccT accum(T const * beg, T const * end)
  21. {
  22. // total 是一个与 AccT 类型所指向的类型相同的局部变量
  23. // zero() 嵌套在 AccumulationTraits<T> 内部函数, 而且 T 是一个模版参数
  24. //typename Traits::AccT total = Traits::zero(); // 获取缺省值, 返回 0 // 存在问题: 当策略为 MultPolicy 会造成结果始终为 0
  25. typename Traits::AccT total = INITVAL; // 把初始值当作【无类型模板参数】传递进来
  26. while (beg != end) // 作累积运算
  27. {
  28. Policy<Traits::AccT, T>::accumulate(total, *beg); // 使用给定的算法策略来进行累积
  29. ++beg;
  30. }
  31. return total; // 返回累积起来的值
  32. }
  33. };
  34.  
  35. //// 用内联的函数模板来包装, 对默认的参数,提供对应的重载函数
  36. template <typename T, const int INITVAL, template <typename, typename> class Policy, typename Traits>
  37. inline typename Traits::AccT accum(T const * beg, T const * end)
  38. {
  39. std::cout << "<typename T, const int INITVAL, template <typename, typename> class Policy, typename Traits> \n\t---> <T, INITVAL, Policy, Traits>" << std::endl; // 标记使用
  40. return Accum<T, INITVAL, Policy, Traits>::accum(beg, end);
  41. }
  42.  
  43. template <typename T, const int INITVAL, template <typename, typename> class Policy>
  44. inline typename AccumulationTraits<T>::AccT accum(T const * beg, T const * end)
  45. {
  46. std::cout << "<typename T, const int INITVAL, template <typename, typename> class Policy> \n\t---> <T, INITVAL, Policy, AccumulationTraits<T>>" << std::endl; // 标记使用
  47. return Accum<T, INITVAL, Policy, AccumulationTraits<T>>::accum(beg, end);
  48. }
  49.  
  50. template <typename T, const int INITVAL>
  51. inline typename AccumulationTraits<T>::AccT accum(T const * beg, T const * end)
  52. {
  53. std::cout << "<typename T, const int INITVAL> \n\t---> <T, INITVAL, MultPolicy, AccumulationTraits<T>>" << std::endl; // 标记使用
  54. return Accum<T, INITVAL, MultPolicy, AccumulationTraits<T>>::accum(beg, end);
  55. }
  56.  
  57. template <typename T>
  58. inline typename AccumulationTraits<T>::AccT accum(T const * beg, T const * end)
  59. {
  60. std::cout << "<typename T> \n\t---> <T, 1, MultPolicy, AccumulationTraits<T>>" << std::endl; // 标记使用
  61. return Accum<T, , MultPolicy, AccumulationTraits<T>>::accum(beg, end);
  62. }
  63.  
  64. template <>
  65. inline typename AccumulationTraits<int>::AccT accum(int const * beg, int const * end)
  66. {
  67. std::cout << "<> \n\t---> <int, 1, MultPolicy, AccumulationTraits<int>>" << std::endl; // 标记使用
  68. return Accum<int, , MultPolicy, AccumulationTraits<int>>::accum(beg, end);
  69. }
  70.  
  71. #endif // !ACCUM_HPP

mytest.cpp // 使用累加算法的客户端测试代码

  1. // 使用累加算法的客户端测试代码
  2. #include "accum.hpp"
  3. #include <iostream>
  4.  
  5. int main()
  6. {
  7. int num[] = {,,,,}; // 整型数组
  8. std::cout << "============= integer array =============" << std::endl;
  9. std::cout << "the total value of the integer values is "
  10. << accum<int, , MultPolicy, AccumulationTraits<int>>(&num[], &num[]) << std::endl;
  11. std::cout << "the total value of the integer values is "
  12. << accum<int, , MultPolicy>(&num[], &num[]) << std::endl;
  13. std::cout << "the total value of the integer values is "
  14. << accum<int, >(&num[], &num[]) << std::endl;
  15. std::cout << "the total value of the integer values is "
  16. << accum<int>(&num[], &num[]) << std::endl;
  17. std::cout << "the total value of the integer values is "
  18. << accum<>(&num[], &num[]) << std::endl;
  19. std::cout << "the total value of the integer values is "
  20. << accum(&num[], &num[]) << std::endl;
  21.  
  22. char name[] = "templates"; // 创建字符值数组
  23. int length = sizeof(name)-;
  24. std::cout << "============= characters array =============" << std::endl;
  25. std::cout << "the total value of the characters in \""
  26. << name << "\" is " << accum<char, , SumPolicy, AccumulationTraits<char>>(&name[], &name[length]) << std::endl;
  27. std::cout << "the total value of the characters in \""
  28. << name << "\" is " << accum<char, , SumPolicy>(&name[], &name[length]) << std::endl;
  29. std::cout << "the total value of the characters in \""
  30. << name << "\" is " << accum<char, >(&name[], &name[length]) << std::endl;
  31. std::cout << "the total value of the characters in \""
  32. << name << "\" is " << accum<char>(&name[], &name[length]) << std::endl;
  33. std::cout << "the total value of the characters in \""
  34. << name << "\" is " << accum<>(&name[], &name[length]) << std::endl;
  35. std::cout << "the total value of the characters in \""
  36. << name << "\" is " << accum(&name[], &name[length]) << std::endl;
  37.  
  38. system("pause");
  39. return ;
  40. }

输出结果:

  1. ============= integer array =============
  2. <typename T, const int INITVAL, template <typename, typename> class Policy, typename Traits>
  3. ---> <T, INITVAL, Policy, Traits>
  4. the total value of the integer values is 120
  5. <typename T, const int INITVAL, template <typename, typename> class Policy>
  6. ---> <T, INITVAL, Policy, AccumulationTraits<T>>
  7. the total value of the integer values is 120
  8. <typename T, const int INITVAL>
  9. ---> <T, INITVAL, MultPolicy, AccumulationTraits<T>>
  10. the total value of the integer values is 120
  11. <>
  12. ---> <int, 1, MultPolicy, AccumulationTraits<int>>
  13. the total value of the integer values is 120
  14. <>
  15. ---> <int, 1, MultPolicy, AccumulationTraits<int>>
  16. the total value of the integer values is 120
  17. <>
  18. ---> <int, 1, MultPolicy, AccumulationTraits<int>>
  19. the total value of the integer values is 120
  20. ============= characters array =============
  21. <typename T, const int INITVAL, template <typename, typename> class Policy, typename Traits>
  22. ---> <T, INITVAL, Policy, Traits>
  23. the total value of the characters in "templates" is 975
  24. <typename T, const int INITVAL, template <typename, typename> class Policy>
  25. ---> <T, INITVAL, Policy, AccumulationTraits<T>>
  26. the total value of the characters in "templates" is 975
  27. <typename T, const int INITVAL>
  28. ---> <T, INITVAL, MultPolicy, AccumulationTraits<T>>
  29. the total value of the characters in "templates" is 0
  30. <typename T>
  31. ---> <T, 1, MultPolicy, AccumulationTraits<T>>
  32. the total value of the characters in "templates" is 465857536
  33. <typename T>
  34. ---> <T, 1, MultPolicy, AccumulationTraits<T>>
  35. the total value of the characters in "templates" is 465857536
  36. <typename T>
  37. ---> <T, 1, MultPolicy, AccumulationTraits<T>>
  38. the total value of the characters in "templates" is 465857536
  39. 请按任意键继续. . .

trait与policy模板应用简单示例的更多相关文章

  1. trait与policy模板技术

    trait与policy模板技术 我们知道,类有属性(即数据)和操作两个方面.同样模板也有自己的属性(特别是模板参数类型的一些具体特征,即trait)和算法策略(policy,即模板内部的操作逻辑). ...

  2. [DeviceOne开发]-轮播图和多模板的简单示例

    一.简介 这个例子是利用Slideview组件实现循环轮播的效果,同时这个slideview作为一个listview的最上面的一行数, 1. listview有2个模板,一个是以slideview为核 ...

  3. [JavaWeb基础] 020.Velocity 模板引擎简单示例

    1.什么是Velocity 一种J2EE的前端模版技术,和JSP,Freemarker差不多,都是用来展示网页内容的.和JSP不同的是velocity只能显示Action中的数据,不能处理数据.不能写 ...

  4. web 框架的本质及自定义web框架 模板渲染jinja2 mvc 和 mtv框架 Django框架的下载安装 基于Django实现的一个简单示例

    Django基础一之web框架的本质 本节目录 一 web框架的本质及自定义web框架 二 模板渲染JinJa2 三 MVC和MTV框架 四 Django的下载安装 五 基于Django实现的一个简单 ...

  5. C++ template —— trait与policy类(七)

    第15章 trait与policy类---------------------------------------------------------------------------------- ...

  6. spring-servlet.xml简单示例

    spring-servlet.xml简单示例 某个项目中的spring-servlet.xml 记下来以后研究用 <!-- springMVC简单配置 --> <?xml versi ...

  7. SignalR 简单示例

    一.什么是 SignalR ASP.NET SignalR is a library for ASP.NET developers that simplifies the process of add ...

  8. Web API 简单示例

    一.RESTful和Web API Representational State Transfer (REST) is a software architecture style consisting ...

  9. asp.net模板控件示例

    原文:asp.net模板控件示例 模板控件允许将控件数据与其表示形式相分离,模板化控件不提供用户界面. 编写它则是为了实现一个命名容器以及包含属性和方法可由宿主页访问的类,MSDN是这样解释的. 下面 ...

随机推荐

  1. git fatal: I don't handle protocol 'https'问题的解决

    问题重现 新建的仓库,再把本地的代码往上push的时候Git提示 $ fatal: I don't handle protocol 'https' 问题分析 Git是支持https的,这点毋庸置疑,所 ...

  2. [python学习笔记] python程序打包成exe文件

    安装 pyinstaller pip3 install pyinstaller 命令 pyinstaller -F -w -i ../ui/icon.ico --clean ../Login.py 参 ...

  3. ArrayList,LinkedListd等容器使用时注意点:

    1.对这两个List(包括其他的类似容器),如果向里面加入一个元素(引用数据类型),那么这个List里面保存的是这个对象的引用: 如果想要避免这种现象可以这样:在加入新的元素时不直接压,将已有的对象复 ...

  4. 小米2017秋招真题——电话号码分身问题(Java版)

    原题描述如下: 通过对各个数字对应的英文单词的分析,可以发现一些规律: 字母Z为0独占,字母W为2独占,字母U为4独占,字母X为6独占,字母G为8独占: 在过滤一遍0.2.4.6.8后,字母O为1独占 ...

  5. Sets 比赛时想错方向了。。。。 (大数不能处理负数啊)

    Sets Time Limit: 6000/3000MS (Java/Others) Memory Limit: 128000/64000KB (Java/Others) SubmitStatus P ...

  6. 提纲挈领webrtc之vad检测

    顾名思义,VAD(Voice Activity Detection)算法的作用是检测是否是人的语音,它的使用 范围极广,降噪,语音识别等领域都需要有vad检测.vad检测有很多方法,这里我们之介绍一 ...

  7. 使用keepalived使用主备热备份功能

    图: 配置文件: 主服务器的配置如下: global_defs { router_id NodeA}vrrp_instance VI_1 { state MASTER #设置为主服务器 interfa ...

  8. OC实现同步访问属性

    有时候,我们在开发过程中需要对属性的访问进行同步操作,这种属性需要做成原子的,用atomic来修饰属性,即可实现这一点. 如果我们想自己实现,可以按照下面方式写代码: SFPerson.h #impo ...

  9. JavaScript--我发现,原来你是这样的JS(四)(看看变量,作用域,垃圾回收机制是啥)

    一.介绍 这是红宝书(JavaScript高级程序设计 3版)的读书笔记第四篇,是红宝书第四章内容(主要是变量和作用域问题),当然其中还有我个人的理解.红宝书这本书可以说是难啃的,要看完不容易,挺厚的 ...

  10. 使用WinDBG调试查看C#内存转储文件

    有时候我们想查看一个正在运行的程序内存中的数据,可以在任务管理器将内存状态保存为转储文件,并使用WinDBG验证,这里我们来试试: 0.安装WinDBG 1.首先写个代码用来测试 一个class pu ...