1. // 11111.cpp : 定义控制台应用程序的入口点。
  2. //
  3.  
  4. #include "stdafx.h"
  5. #include <iostream>
  6. #include <string>
  7.  
  8. template<typename T>
  9. class AccumulationTraits;
  10.  
  11. template<>
  12. class AccumulationTraits<char> {
  13. public:
  14. typedef int Acct;
  15. static Acct zero() {
  16. return 0;
  17. }
  18. };
  19.  
  20. template<>
  21. class AccumulationTraits<int> {
  22. public:
  23. typedef long Acct;
  24. static Acct zero() {
  25. return 0;
  26. }
  27. };
  28.  
  29. template<>
  30. class AccumulationTraits<float> {
  31. public:
  32. typedef double Acct;
  33. static Acct zero() {
  34. return 0;
  35. }
  36. };
  37. /*
  38. template<typename T,typename AT = AccumulationTraits<T> >
  39. class Accum {
  40. public:
  41. static typename AT::Acct accum(T const* beg, T const* end) {
  42. typename AT::Acct total = AT::zero();
  43. while (beg != end)
  44. {
  45. total += *beg;
  46. ++beg;
  47. }
  48. return total;
  49. }
  50. };
  51.  
  52. template<typename T>
  53. typename AccumulationTraits<T>::Acct accum(T const* beg, T const* end)
  54. {
  55. std::cout << "T" << std::endl;
  56. return Accum<T>::accum(beg,end);
  57. }
  58.  
  59. template<typename Traits, typename T>
  60. typename AccumulationTraits<T>::Acct accum(T const* beg, T const* end)
  61. {
  62. std::cout << "T Traits" << std::endl;
  63. return Accum<T,Traits>::accum(beg, end);
  64. }
  65. */
  66. template<typename T1, typename T2>
  67. class SumPolicy {
  68. public:
  69. static void accumulate(T1& total, T2 const& value) {
  70. total += value;
  71. }
  72. };
  73.  
  74. template<typename T,
  75. template<typename,typename>class Policy = SumPolicy,
  76. typename Traits = AccumulationTraits<T> >
  77. class Accum {
  78. public:
  79. typedef typename Traits::Acct Acct;
  80. static Acct accum(T const* beg,T const* end) {
  81. Acct total = Traits::zero();
  82. while (beg != end)
  83. {
  84. Policy<Acct, T>::accumulate(total, *beg);
  85. ++beg;
  86. }
  87. return total;
  88. }
  89.  
  90. };
  91.  
  92. int main()
  93. {
  94. int num[] = { 1,2,3,4,5 };
  95. std::cout << Accum<int>::accum(&num[0],&num[5]) << std::endl;
  96.  
  97. return 0;
  98. }

  

  1. // 11111.cpp : 定义控制台应用程序的入口点。
  2. //
  3.  
  4. #include "stdafx.h"
  5. #include <iostream>
  6. #include <string>
  7.  
  8. template<typename T>
  9. class AccumulationTraits;
  10.  
  11. template<>
  12. class AccumulationTraits<char> {
  13. public:
  14. typedef int Acct;
  15. static Acct zero() {
  16. return 0;
  17. }
  18. };
  19.  
  20. template<>
  21. class AccumulationTraits<int> {
  22. public:
  23. typedef long Acct;
  24. static Acct zero() {
  25. return 0;
  26. }
  27. };
  28.  
  29. template<>
  30. class AccumulationTraits<float> {
  31. public:
  32. typedef double Acct;
  33. static Acct zero() {
  34. return 0;
  35. }
  36. };
  37. /*
  38. template<typename T,typename AT = AccumulationTraits<T> >
  39. class Accum {
  40. public:
  41. static typename AT::Acct accum(T const* beg, T const* end) {
  42. typename AT::Acct total = AT::zero();
  43. while (beg != end)
  44. {
  45. total += *beg;
  46. ++beg;
  47. }
  48. return total;
  49. }
  50. };
  51.  
  52. template<typename T>
  53. typename AccumulationTraits<T>::Acct accum(T const* beg, T const* end)
  54. {
  55. std::cout << "T" << std::endl;
  56. return Accum<T>::accum(beg,end);
  57. }
  58.  
  59. template<typename Traits, typename T>
  60. typename AccumulationTraits<T>::Acct accum(T const* beg, T const* end)
  61. {
  62. std::cout << "T Traits" << std::endl;
  63. return Accum<T,Traits>::accum(beg, end);
  64. }
  65. */
  66. template<bool b = true>
  67. class SumPolicy {
  68. public:
  69. template<typename T1, typename T2>
  70. static void accumulate(T1& total, T2 const& value) {
  71. total += value;
  72. }
  73. };
  74.  
  75. template<>
  76. class SumPolicy<false> {
  77. public:
  78. template<typename T1, typename T2>
  79. static void accumulate(T1& total, T2 const& value) {
  80. total = total+value;
  81. }
  82. };
  83.  
  84. template <typename T,
  85. typename Policy = SumPolicy<>,
  86. typename Traits = AccumulationTraits<T> >
  87. class Accum {
  88. public:
  89. typedef typename Traits::Acct Acct;
  90. static Acct accum(T const* beg, T const* end) {
  91. Acct total = Traits::zero();
  92. while (beg != end) {
  93. Policy::accumulate(total, *beg);
  94. ++beg;
  95. }
  96. return total;
  97. }
  98. };
  99.  
  100. int main()
  101. {
  102. int num[] = { 1,2,3,4,5 };
  103. std::cout << Accum<int>::accum(&num[0], &num[5]) << std::endl;
  104. std::cout << Accum<int,SumPolicy<false>>::accum(&num[0],&num[5]) << std::endl;
  105. std::cout << Accum<int, SumPolicy<true>>::accum(&num[0], &num[5]) << std::endl;
  106.  
  107. return 0;
  108. }

  

  1. // 111111.cpp : 定义控制台应用程序的入口点。
  2.  
  3. //
  4.  
  5. #include "stdafx.h"
  6. #include <iostream>
  7.  
  8. template<typename T>
  9. class IsFundaT {
  10. public:
  11. enum{Yes = 0,No=1};
  12. };
  13.  
  14. #define MK_FUNDA_TYPE(T) \
  15. template<> class IsFundaT<T> { \
  16. public: \
  17. enum{Yes =1,No=0}; \
  18. };
  19. MK_FUNDA_TYPE(void)
  20. MK_FUNDA_TYPE(bool)
  21. MK_FUNDA_TYPE(signed char)
  22. MK_FUNDA_TYPE(unsigned char)
  23. MK_FUNDA_TYPE(wchar_t)
  24.  
  25. MK_FUNDA_TYPE(signed short)
  26. MK_FUNDA_TYPE(unsigned short)
  27. MK_FUNDA_TYPE(signed int)
  28. MK_FUNDA_TYPE(unsigned int)
  29. MK_FUNDA_TYPE(signed long)
  30. MK_FUNDA_TYPE(unsigned long)
  31. #if LONGLONG_EXISTS
  32. MK_FUNDA_TYPE(signed long long)
  33. MK_FUNDA_TYPE(unsigned long long)
  34. #endif // LONGLONG_EXISTS
  35.  
  36. MK_FUNDA_TYPE(float)
  37. MK_FUNDA_TYPE(double)
  38. MK_FUNDA_TYPE(long double)
  39.  
  40. //======================================================
  41.  
  42. template<typename T>
  43. void test(T const& t) {
  44. if (IsFundaT<T>::Yes) {
  45. std::cout << "T is fundamental type" << std::endl;
  46. }
  47. else
  48. {
  49. std::cout << "T is no fundamental type" << std::endl;
  50. }
  51. }
  52.  
  53. class MyType{ };
  54.  
  55. int main()
  56. {
  57. int i = 9;
  58. auto ii = std::move(i);
  59. test(ii);
  60. test(7);
  61. test(MyType());
  62.  
  63. return 0;
  64. }

  

模板学习实践一 accumulationtraits的更多相关文章

  1. 模板学习实践二 pointer

    c++ template学习记录 使用模板将实际类型的指针进行封装 当变量退出作用域 自动delete // 1111.cpp : 定义控制台应用程序的入口点. // #include "s ...

  2. 模板学习实践三 functor

    #include <iostream>#include <typeinfo> void foo(){ std::cout << "foo() called ...

  3. Nagios学习实践系列——配置研究[监控当前服务器]

    其实上篇Nagios学习实践系列——基本安装篇只是安装了Nagios基本组件,虽然能够打开主页,但是如果不配置相关配置文件文件,那么左边菜单很多页面都打不开,相当于只是一个空壳子.接下来,我们来学习研 ...

  4. Nagios学习实践系列

    其实上篇Nagios学习实践系列--基本安装篇只是安装了Nagios基本组件,虽然能够打开主页,但是如果不配置相关配置文件文件,那么左边菜单很多页面都打不开,相当于只是一个空壳子.接下来,我们来学习研 ...

  5. 第04项目:淘淘商城(SpringMVC+Spring+Mybatis) 的学习实践总结【第四天】

    https://pan.baidu.com/s/1bptYGAb#list/path=%2F&parentPath=%2Fsharelink389619878-229862621083040 ...

  6. 使用sklearn进行集成学习——实践

    系列 <使用sklearn进行集成学习——理论> <使用sklearn进行集成学习——实践> 目录 1 Random Forest和Gradient Tree Boosting ...

  7. Nagios学习实践系列——基本安装篇

    开篇介绍 最近由于工作需要,学习研究了一下Nagios的安装.配置.使用,关于Nagios的介绍,可以参考我上篇随笔Nagios学习实践系列——产品介绍篇 实验环境 操作系统:Red Hat Ente ...

  8. 前端学习实践笔记--JavaScript深入【1】

    这一年中零零散散看过几本javascript的书,回过头看之前写过的javascript学习笔记,未免有点汗颜,突出“肤浅”二字,然越深入越觉得javascript的博大精深,有种只缘身在此山中的感觉 ...

  9. Appium学习实践(四)结构优化

    随着我们测试脚本中的用例越来越多,我们不可能将所有的用例都放在同一个脚本中,所以我们需要优化我们的结构.将脚本放在一个文件夹中,再通过别的脚本来执行脚本.这样,我们也可以有选择性的执行我们的脚本 先来 ...

随机推荐

  1. os.rename 和os.replace

    f1 = open("hello.txt","w") f1.write("hello,my name is bobo.") f1.close ...

  2. flutter环境配置

    java环境安装 做基于android的原生app,首先需要安装java环境,需要到官网https://www.oracle.com/technetwork/java/javase/downloads ...

  3. Scrapy学习篇(十一)之设置随机User-Agent

    大多数情况下,网站都会根据我们的请求头信息来区分你是不是一个爬虫程序,如果一旦识别出这是一个爬虫程序,很容易就会拒绝我们的请求,因此我们需要给我们的爬虫手动添加请求头信息,来模拟浏览器的行为,但是当我 ...

  4. html/css/js-横向滚动条的实现

    在前端UI设计时,网页的制作很麻烦,深有感悟!碰到太多的不懂,或是第一次见,就要去网上找资料!横向滚动条就是我遇到麻烦中其中的一个,其实也 很简单,只是在几次项目中都用到了这个横向滚动条所以就拿出来说 ...

  5. 关于ARM Linux下的SD卡及U盘的挂载问题

    内核配置并运行后,挂载SD卡,出现问题: zynq> mount -t /dev/mmcblk1 /mntmount: mounting /dev/mmcblk0 on /mnt failed: ...

  6. IDEA查看类继承关系及生成类关系图

    1.在想要查看的类上按 Ctrl + H -> Diagrams -> Show Diagrams -> Java Class Diagrams -> Show Impleme ...

  7. Kafka命令操作

    本文主要介绍Kafka的shell命令: 查看当前服务器所有的topic [hadoop@datanode1 kafka]$ bin/kafka-topics.sh --zookeeper datan ...

  8. python使用 openpyxl包 excel读取与写入

    '''### 写入操作 ###from openpyxl import Workbook#实例化对象wb=Workbook()#创建表ws1=wb.create_sheet('work',0) #默认 ...

  9. 最简单的cmd命令行取得系统路径和python的安装路径(适用于winxp.win7和win10)

    @echo off::pip install seleniumpython -c"import sys;print(sys.prefix)" >temp.txtfor /f ...

  10. python常用库,包网址

    常用包下载:https://pypi.org/ 1.NumPy: https://www.numpy.org/ 2.pandas: http://pandas.pydata.org/ 3.SciPy: ...