• 模板学习实践一 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;
    }
    

      

  • 相关阅读:
    获取网页可视区域的宽高,不包括工具栏和滚动条(浏览器窗口可视区域大小)
    display:inline-block 来解决盒子高度不一样,造成的盒子浮动
    Jquery UI 中的datepicker() ,获取日期后的回调函数onClose()
    删除url搜索带的参数
    判断表单是否需要校验时可以按照类似逻辑去处理
    去除url中锚点的方法
    js常面试基础题
    vue项目总结
    vue-router
    回流与重绘
  • 原文地址:https://www.cnblogs.com/itdef/p/5721814.html
Copyright © 2020-2023  润新知