模板Policy
来源:互联网 发布:apache cxf 配置文件 编辑:程序博客网 时间:2024/06/05 10:20
Policy用来制定算法采取的策略,使用Policy可以是的算法的策略可以修改,控制粒度更加的小。
#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; }};class SumPolicy {public:template<typename T1, typename T2>static void accumulate(T1 &total, const T2 &value) {total += value;}};class MultiPolicy {public:template<typename T1, typename T2>static void accumulate(T1 &total, const T2 &value) {total *= value;}};template<typename T, typename Policy = SumPolicy, 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 ) {Policy::accumulate(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, SumPolicy, Traits>::accumulate( begin, end );}template<typename Policy, typename Traits, typename T>typename Traits::AccT accumulate( const T *begin, const T *end ){return Accum<T, Policy, Traits>::accumulate(begin, end);}#endif
int iv[5] = {1, 2, 3, 4, 5};double ftotal = accumulate<MultiPolicy, typename AccumulationTraits<float>, int>( iv, iv + sizeof( iv ) / sizeof( int ) );EXPECT_EQ( 0, ftotal );char cv[] = {'a', 'a', 'b', 'b'};int total = 97 * 2 + 98 * 2;EXPECT_EQ( total , accumulate( cv, cv + sizeof( cv ) / sizeof( char ) ) );
- 模板Policy
- C++模板Policy
- C++模板 - policy类
- C++模板 - traits & policy
- C++模板 - policy类
- trait与policy模板技术
- 第一章policy(特征,模板中的模板类)
- Step By Step(C++模板Policy)
- C++ 模板设计 policy和traits
- c++ 基于Policy 的 模板编程
- 模板特性(trait)和政策(policy)
- 模板:policy类的简单使用
- policy
- 模板:用双重模板参数实现 简单的 policy 类
- 我的C++实践(4):trait与policy模板技术
- 我的C++实践(4):trait与policy模板技术
- jaas policy
- Audio Policy
- sql语句之查询与删除重复数据
- liunx相关网站
- 黑马程序员------面向对象(No.2)(封装、构造函数、构造代码块、this)
- JavaScript初学者应知的24条最佳实践(译)
- C#LDAP删除用户
- 模板Policy
- MapReduce编程(入门篇)
- hdu(4710) Balls Rearrangement
- 使用 Jrebel 插件实现热加载
- UIApplication深入研究
- Openstack 是邻家男孩?还是洪水猛兽?
- 浅谈矩阵分解在推荐系统中的应用(转发)
- iOS6和iPhone5使用第三方静态库在真机无法运行的处理
- C++自删除