C ++特征和策略

时间:2016-04-15 14:12:40

标签: c++ templates

我正在研究“C ++模板:完整指南”中的一个例子 作者:David Vandevoorde,Nicolai M. Josuttis“并改变了下面的一些代码。我无法理解为什么它不能编译。有什么帮助吗?(下面的错误信息)

    // Example program
#include <iostream>
#include <string>
#include <vector>
#include <bitset>
#include <deque>
#include <stdexcept>

template<typename T>
class AccumulationTraits;

template<>
class AccumulationTraits<int>{
    public:
        typedef long Ac;
        static Ac zero(){
            return 0;
        }
};

template<>
class AccumulationTraits<float>{
    public:
        typedef double Ac;
        static Ac zero(){
            return 0;
        }
};

template<typename T,typename Trait=AccumulationTraits<T>>
class SumPolicy{
    public:
        static typename Trait::Ac accumulate(typename Trait::Ac& total ,const T& beg){
            return total+=beg;
        }
};

template<typename T, typename Trait=AccumulationTraits<T>>
class MultPoliciy{
    public:
        static typename Trait::Ac accumulate(typename Trait::Ac& total, const T& beg){
            return total*=beg;
        }
};


template<typename T, template<typename, typename> class Policy=SumPolicy, typename Traits=AccumulationTraits<T>>
class Accum{
    public:
         static typename Traits::Ac accum(const T* beg, const T* end){
             typename Traits::Ac total=Traits::zero();
             while(beg!=end){
                 Policy<T>::accumulate(total, *beg);
                 ++beg;
             }
             return total;
         }
};


template<typename T, template<typename, typename> class Policy=SumPolicy>
inline typename AccumulationTraits<T>::Ac accum(const T* beg, const T* end){
    return Accum<T, Policy>::accum(beg, end);
}

template<typename T, template<typename> class AccumulationTraits,  template <typename, typename> class Policy=SumPolicy>
inline typename AccumulationTraits<T>::Ac accum(const T* beg, const T* end){
    return Accum<T, Policy, AccumulationTraits>::accum(beg,end);
}

int main()
{
    int num[]={1,2,3,4,5};
    std::cout << "The average is : " << accum<int, MultPoliciy>(&num[0], &num[5]) << std::endl;
}

错误信息是:

  source_file.cpp(53): error C2976: 'MultPoliciy': too few template arguments
source_file.cpp(39): note: see declaration of 'MultPoliciy'
source_file.cpp(50): note: while compiling class template member function 'AccumulationTraits<int>::Ac Accum<T,Policy,AccumulationTraits<int>>::accum(const T *,const T *)'
        with
        [
            T=int,
            Policy=MultPoliciy
        ]
source_file.cpp(63): note: see reference to function template instantiation 'AccumulationTraits<int>::Ac Accum<T,Policy,AccumulationTraits<int>>::accum(const T *,const T *)' being compiled
        with
        [
            T=int,
            Policy=MultPoliciy
        ]
source_file.cpp(63): note: see reference to class template instantiation 'Accum<T,Policy,AccumulationTraits<int>>' being compiled
        with
        [
            T=int,
            Policy=MultPoliciy
        ]
source_file.cpp(74): note: see reference to function template instantiation 'AccumulationTraits<int>::Ac accum<int,MultPoliciy>(const T *,const T *)' being compiled
        with
        [
            T=int
        ]
source_file.cpp(53): error C2955: 'MultPoliciy': use of class template requires template argument list
source_file.cpp(39): note: see declaration of 'MultPoliciy'
source_file.cpp(53): error C2670: 'MultPoliciy<T,Trait>::accumulate': the function template cannot convert parameter 1 from type 'AccumulationTraits<int>::Ac'

0 个答案:

没有答案