模板的Traits
Traits含义就是特性,应用Trait模板参数,使得我们的程序既保持灵活性,同时减少类型参数的数量。能够使得我们对函数进行更加细粒度的控制。
#ifndef TRAIT_H_
#define TRAIT_H_
/*
template<typename T>
T accumulate( const T *begin, const T *end )
{
T total = T(); while ( begin != end ) {
total += *begin;
++begin;
} return total;
}*/ template<typename T>
class AccumulationTraits; template<>
class AccumulationTraits<char>
{
public:
typedef int AccT;
static AccT zero() {
return 0;
}
}; template<>
class AccumulationTraits<short>
{
public:
typedef int AccT;
static AccT zero() {
return 0;
}
}; template<>
class AccumulationTraits<int>
{
public:
typedef long long AccT;
static AccT zero() {
return 0;
}
}; template<>
class AccumulationTraits<unsigned int>
{
public:
typedef unsigned long 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 accumulate( const T *begin, const T *end ) {
typename AT::AccT total = AT::zero(); while ( begin != end ) {
total += *begin;
++begin;
} return total;
}
}; template<typename T>
typename AccumulationTraits<T>::AccT accumulate( const T *begin,
const T *end )
{
return Accum<T>::accumulate( begin, end );
} template<typename Traits, typename T>
typename Traits::AccT accumulate( const T *begin, const T *end )
{
return Accum<T, Traits>::accumulate( begin, end );
} #endif
int iv[5] = {1, 2, 3, 4, 5};
double ftotal = accumulate<typename AccumulationTraits<float>, int>( iv, iv + sizeof( iv ) / sizeof( int ) );
EXPECT_EQ( 15, ftotal );
char cv[] = {'a', 'a', 'b', 'b'};
int total = 97 * 2 + 98 * 2;
EXPECT_EQ( total , accumulate( cv, cv + sizeof( cv ) / sizeof( char ) ) );
模板的Traits的更多相关文章
- c++ 模板和traits
#define TEST(ITEMNAME) AddItem(ITEMNAME, #ITEMNAME); template <typename T> void AddItem(T& ...
- 【C++模版之旅】项目中一次活用C++模板(traits)的经历
曾经曾在一个项目中碰到过一个挺简单的问题,但一时又不能用普通常规的方法去非常好的解决,最后通过C++模板的活用,通过traits相对照较巧妙的攻克了这个问题.本文主要想重现问题发生,若干解决方式的比較 ...
- trait与policy模板应用简单示例
trait与policy模板应用简单示例 accumtraits.hpp // 累加算法模板的trait // 累加算法模板的trait #ifndef ACCUMTRAITS_HPP #define ...
- 初识C++模板元编程(Template Mega Programming)
前言:毕设时在开源库上做的程序,但是源码看得很晕(当时导师告诉我这是模板元编程,可以不用太在乎),最近自己造轮子时想学习STL的源码,但也是一样的感觉,大致了解他这么做要干什么,但是不知道里面的机制. ...
- [转载] C++ 多线程编程总结
原文: http://www.cnblogs.com/zhiranok/archive/2012/05/13/cpp_multi_thread.html 在开发C++程序时,一般在吞吐量.并发.实时性 ...
- 跨平台渲染框架尝试 - Texture管理
纹理是渲染器重要的资源,也是比较简单的资源.本文将详细讨论纹理资源的管理. 在资源管理概述中提到,资源就是一堆内存和CPU与GPU的访问权限.纹理管理在资源管理之上,要负责如何使用者一堆内存构造纹理对 ...
- 转 c++多线程编程
c++多线程编程 一直对多线程编程这一块很陌生,决定花一点时间整理一下. os:ubuntu 10.04 c++ 1.最基础,进程同时创建5个线程,各自调用同一个函数 #include <io ...
- [转]Traits 编程技法+模板偏特化+template参数推导+内嵌型别编程技巧
STL中,traits编程技法得到了很大的应用,了解这个,才能一窥STL奥妙所在. 先将自己所理解的记录如下: Traits技术可以用来获得一个 类型 的相关信息的. 首先假如有以下一个泛型的迭代器类 ...
- c/c++ 模板与STL小例子系列<三> traits
c/c++ 模板与STL小例子系列 traits 对这个概念,还是处于懵逼的状态,初步体会就是,为了解决类型之间的转换问题. 从一个类型为A的指针,转化到类型为B的指针,中间需要用void*来作为中介 ...
随机推荐
- Fix Windows 7 Msvcp71.dll And Msvcr71.dll Missing Error
Fix Windows 7 Msvcp71.dll And Msvcr71.dll Missing Error Fix Msvcp71.dll And Msvcr71.dll Missing Erro ...
- lsh341999的资源
懒得下载了,仔细研究: http://download.csdn.net/user/lsh341999/uploads/2
- android——使用pull解析xml文件
1.persons.xml 将persons.xml文件放到src目录下.其代码如下: <?xml version='1.0' encoding='UTF-8' standalone='yes' ...
- 《Swift Programming Language 》——Swift中怎样使用继承(Inheritance)
一个类能够继承(inherit)还有一个类的方法(methods),属性(property)和其他特性.当一个类继承其他类时,继承类叫子类(subclass),被继承类叫超类(或父类,supercla ...
- MSSQL、C# 、Winform、ASP.NET - 数据库备份与还原模块
数据库备份还原类: using System; using System.Collections.Generic; using System.Linq; using System.Text; usin ...
- js数组基础整理
首页: 主要整理了一下数组中常用的一些基础知识,代码都是自己手敲,有不对的地方希望能指出,目前只有4篇,后续会不断的增加这一板块. 由于少于100字不能发所以把一些最基本的创建数组也写上. // 创建 ...
- perl 继承小例子
<pre name="code" class="html"><pre name="code" class="ht ...
- 深入浅出Hadoop实战开发(HDFS实战图片、MapReduce、HBase实战微博、Hive应用)
Hadoop是什么,为什么要学习Hadoop? Hadoop是一个分布式系统基础架构,由Apache基金会开发.用户可以在不了解分布式底层细节的情况下,开发分布式程序.充分利用集群的威力高速运 ...
- boost::asio async_write也不能保证一次发完所有数据 一
你要是看过basic_stream_socket的文档,里面提到async_write_some不能保证将所有要发送的数据都发出去.并且提到如果想这样做,需要使用boost asio的async_wr ...
- u盘安装ubuntu10.04 server.txt
10.04 先将 ubuntu server 的 iso 放到优盘上,然后在提示无法找到光驱时,按 alt+f2 打开一个新的 console 窗口,将 iso mount 上,具体操作如下: ls ...