模板学习实践一 accumulationtraits
- // 11111.cpp : 定义控制台应用程序的入口点。
- //
- #include "stdafx.h"
- #include <iostream>
- #include <string>
- template<typename T>
- class AccumulationTraits;
- template<>
- class AccumulationTraits<char> {
- public:
- typedef int Acct;
- static Acct zero() {
- return 0;
- }
- };
- template<>
- class AccumulationTraits<int> {
- public:
- typedef long Acct;
- static Acct zero() {
- return 0;
- }
- };
- template<>
- class AccumulationTraits<float> {
- public:
- typedef double Acct;
- static Acct zero() {
- return 0;
- }
- };
- /*
- template<typename T,typename AT = AccumulationTraits<T> >
- class Accum {
- public:
- static typename AT::Acct accum(T const* beg, T const* end) {
- typename AT::Acct total = AT::zero();
- while (beg != end)
- {
- total += *beg;
- ++beg;
- }
- return total;
- }
- };
- template<typename T>
- typename AccumulationTraits<T>::Acct accum(T const* beg, T const* end)
- {
- std::cout << "T" << std::endl;
- return Accum<T>::accum(beg,end);
- }
- template<typename Traits, typename T>
- typename AccumulationTraits<T>::Acct accum(T const* beg, T const* end)
- {
- std::cout << "T Traits" << std::endl;
- return Accum<T,Traits>::accum(beg, end);
- }
- */
- template<typename T1, typename T2>
- class SumPolicy {
- public:
- static void accumulate(T1& total, T2 const& value) {
- total += value;
- }
- };
- template<typename T,
- template<typename,typename>class Policy = SumPolicy,
- typename Traits = AccumulationTraits<T> >
- class Accum {
- public:
- typedef typename Traits::Acct Acct;
- static Acct accum(T const* beg,T const* end) {
- Acct total = Traits::zero();
- while (beg != end)
- {
- Policy<Acct, T>::accumulate(total, *beg);
- ++beg;
- }
- return total;
- }
- };
- int main()
- {
- int num[] = { 1,2,3,4,5 };
- std::cout << Accum<int>::accum(&num[0],&num[5]) << std::endl;
- return 0;
- }
- // 11111.cpp : 定义控制台应用程序的入口点。
- //
- #include "stdafx.h"
- #include <iostream>
- #include <string>
- template<typename T>
- class AccumulationTraits;
- template<>
- class AccumulationTraits<char> {
- public:
- typedef int Acct;
- static Acct zero() {
- return 0;
- }
- };
- template<>
- class AccumulationTraits<int> {
- public:
- typedef long Acct;
- static Acct zero() {
- return 0;
- }
- };
- template<>
- class AccumulationTraits<float> {
- public:
- typedef double Acct;
- static Acct zero() {
- return 0;
- }
- };
- /*
- template<typename T,typename AT = AccumulationTraits<T> >
- class Accum {
- public:
- static typename AT::Acct accum(T const* beg, T const* end) {
- typename AT::Acct total = AT::zero();
- while (beg != end)
- {
- total += *beg;
- ++beg;
- }
- return total;
- }
- };
- template<typename T>
- typename AccumulationTraits<T>::Acct accum(T const* beg, T const* end)
- {
- std::cout << "T" << std::endl;
- return Accum<T>::accum(beg,end);
- }
- template<typename Traits, typename T>
- typename AccumulationTraits<T>::Acct accum(T const* beg, T const* end)
- {
- std::cout << "T Traits" << std::endl;
- return Accum<T,Traits>::accum(beg, end);
- }
- */
- template<bool b = true>
- class SumPolicy {
- public:
- template<typename T1, typename T2>
- static void accumulate(T1& total, T2 const& value) {
- total += value;
- }
- };
- template<>
- class SumPolicy<false> {
- public:
- template<typename T1, typename T2>
- static void accumulate(T1& total, T2 const& value) {
- total = total+value;
- }
- };
- template <typename T,
- typename Policy = SumPolicy<>,
- typename Traits = AccumulationTraits<T> >
- class Accum {
- public:
- typedef typename Traits::Acct Acct;
- static Acct accum(T const* beg, T const* end) {
- Acct total = Traits::zero();
- while (beg != end) {
- Policy::accumulate(total, *beg);
- ++beg;
- }
- return total;
- }
- };
- int main()
- {
- int num[] = { 1,2,3,4,5 };
- std::cout << Accum<int>::accum(&num[0], &num[5]) << std::endl;
- std::cout << Accum<int,SumPolicy<false>>::accum(&num[0],&num[5]) << std::endl;
- std::cout << Accum<int, SumPolicy<true>>::accum(&num[0], &num[5]) << std::endl;
- return 0;
- }
- // 111111.cpp : 定义控制台应用程序的入口点。
- //
- #include "stdafx.h"
- #include <iostream>
- template<typename T>
- class IsFundaT {
- public:
- enum{Yes = 0,No=1};
- };
- #define MK_FUNDA_TYPE(T) \
- template<> class IsFundaT<T> { \
- public: \
- enum{Yes =1,No=0}; \
- };
- MK_FUNDA_TYPE(void)
- MK_FUNDA_TYPE(bool)
- MK_FUNDA_TYPE(signed char)
- MK_FUNDA_TYPE(unsigned char)
- MK_FUNDA_TYPE(wchar_t)
- MK_FUNDA_TYPE(signed short)
- MK_FUNDA_TYPE(unsigned short)
- MK_FUNDA_TYPE(signed int)
- MK_FUNDA_TYPE(unsigned int)
- MK_FUNDA_TYPE(signed long)
- MK_FUNDA_TYPE(unsigned long)
- #if LONGLONG_EXISTS
- MK_FUNDA_TYPE(signed long long)
- MK_FUNDA_TYPE(unsigned long long)
- #endif // LONGLONG_EXISTS
- MK_FUNDA_TYPE(float)
- MK_FUNDA_TYPE(double)
- MK_FUNDA_TYPE(long double)
- //======================================================
- template<typename T>
- void test(T const& t) {
- if (IsFundaT<T>::Yes) {
- std::cout << "T is fundamental type" << std::endl;
- }
- else
- {
- std::cout << "T is no fundamental type" << std::endl;
- }
- }
- class MyType{ };
- int main()
- {
- int i = 9;
- auto ii = std::move(i);
- test(ii);
- test(7);
- test(MyType());
- return 0;
- }
模板学习实践一 accumulationtraits的更多相关文章
- 模板学习实践二 pointer
c++ template学习记录 使用模板将实际类型的指针进行封装 当变量退出作用域 自动delete // 1111.cpp : 定义控制台应用程序的入口点. // #include "s ...
- 模板学习实践三 functor
#include <iostream>#include <typeinfo> void foo(){ std::cout << "foo() called ...
- Nagios学习实践系列——配置研究[监控当前服务器]
其实上篇Nagios学习实践系列——基本安装篇只是安装了Nagios基本组件,虽然能够打开主页,但是如果不配置相关配置文件文件,那么左边菜单很多页面都打不开,相当于只是一个空壳子.接下来,我们来学习研 ...
- Nagios学习实践系列
其实上篇Nagios学习实践系列--基本安装篇只是安装了Nagios基本组件,虽然能够打开主页,但是如果不配置相关配置文件文件,那么左边菜单很多页面都打不开,相当于只是一个空壳子.接下来,我们来学习研 ...
- 第04项目:淘淘商城(SpringMVC+Spring+Mybatis) 的学习实践总结【第四天】
https://pan.baidu.com/s/1bptYGAb#list/path=%2F&parentPath=%2Fsharelink389619878-229862621083040 ...
- 使用sklearn进行集成学习——实践
系列 <使用sklearn进行集成学习——理论> <使用sklearn进行集成学习——实践> 目录 1 Random Forest和Gradient Tree Boosting ...
- Nagios学习实践系列——基本安装篇
开篇介绍 最近由于工作需要,学习研究了一下Nagios的安装.配置.使用,关于Nagios的介绍,可以参考我上篇随笔Nagios学习实践系列——产品介绍篇 实验环境 操作系统:Red Hat Ente ...
- 前端学习实践笔记--JavaScript深入【1】
这一年中零零散散看过几本javascript的书,回过头看之前写过的javascript学习笔记,未免有点汗颜,突出“肤浅”二字,然越深入越觉得javascript的博大精深,有种只缘身在此山中的感觉 ...
- Appium学习实践(四)结构优化
随着我们测试脚本中的用例越来越多,我们不可能将所有的用例都放在同一个脚本中,所以我们需要优化我们的结构.将脚本放在一个文件夹中,再通过别的脚本来执行脚本.这样,我们也可以有选择性的执行我们的脚本 先来 ...
随机推荐
- os.rename 和os.replace
f1 = open("hello.txt","w") f1.write("hello,my name is bobo.") f1.close ...
- flutter环境配置
java环境安装 做基于android的原生app,首先需要安装java环境,需要到官网https://www.oracle.com/technetwork/java/javase/downloads ...
- Scrapy学习篇(十一)之设置随机User-Agent
大多数情况下,网站都会根据我们的请求头信息来区分你是不是一个爬虫程序,如果一旦识别出这是一个爬虫程序,很容易就会拒绝我们的请求,因此我们需要给我们的爬虫手动添加请求头信息,来模拟浏览器的行为,但是当我 ...
- html/css/js-横向滚动条的实现
在前端UI设计时,网页的制作很麻烦,深有感悟!碰到太多的不懂,或是第一次见,就要去网上找资料!横向滚动条就是我遇到麻烦中其中的一个,其实也 很简单,只是在几次项目中都用到了这个横向滚动条所以就拿出来说 ...
- 关于ARM Linux下的SD卡及U盘的挂载问题
内核配置并运行后,挂载SD卡,出现问题: zynq> mount -t /dev/mmcblk1 /mntmount: mounting /dev/mmcblk0 on /mnt failed: ...
- IDEA查看类继承关系及生成类关系图
1.在想要查看的类上按 Ctrl + H -> Diagrams -> Show Diagrams -> Java Class Diagrams -> Show Impleme ...
- Kafka命令操作
本文主要介绍Kafka的shell命令: 查看当前服务器所有的topic [hadoop@datanode1 kafka]$ bin/kafka-topics.sh --zookeeper datan ...
- python使用 openpyxl包 excel读取与写入
'''### 写入操作 ###from openpyxl import Workbook#实例化对象wb=Workbook()#创建表ws1=wb.create_sheet('work',0) #默认 ...
- 最简单的cmd命令行取得系统路径和python的安装路径(适用于winxp.win7和win10)
@echo off::pip install seleniumpython -c"import sys;print(sys.prefix)" >temp.txtfor /f ...
- python常用库,包网址
常用包下载:https://pypi.org/ 1.NumPy: https://www.numpy.org/ 2.pandas: http://pandas.pydata.org/ 3.SciPy: ...