C++ traints usage,该如何解决
C++ traints usage
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 AccT;
static AccT zero(){
return 0;
}
};
/*
template <typename T>
inline
typename AccumulationTraits<T>::AccT accum(T const * beg,T const * end)
{
typedef typename AccumulationTraits<T>::AccT AccT;
AccT total = AccumulationTraits<T>::zero();//AccT();
while(beg != end){
total += *beg;
++beg;
}
return total;
}
*/
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 Traints,typename T>
inline
typename Traints::AccT accum(T const * beg,T const * end)
{
return Accum<T,Traints>::accum(beg,end);
}
int main()
{
const int num[5] = {1,2,3,4,5};
//accum(&num[0],&num[4]);
typedef Accum <char> test;
test testabc;
const char testchar[8] = "abcdefg";
// testabc(&testchar[0],&testchar[7]);
char testa = 'a';
char testb = 'b';
const char * ptesta = &testa;
const char * ptestb = &testb;
//testabc(ptesta,ptestb);
return 0;
}
----------------------------------------------
----------------------------------------------
I got this program on net. but the testabc(ptesta,ptestb) function could not be used correctly,
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 AccT;
static AccT zero(){
return 0;
}
};
/*
template <typename T>
inline
typename AccumulationTraits<T>::AccT accum(T const * beg,T const * end)
{
typedef typename AccumulationTraits<T>::AccT AccT;
AccT total = AccumulationTraits<T>::zero();//AccT();
while(beg != end){
total += *beg;
++beg;
}
return total;
}
*/
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 Traints,typename T>
inline
typename Traints::AccT accum(T const * beg,T const * end)
{
return Accum<T,Traints>::accum(beg,end);
}
int main()
{
const int num[5] = {1,2,3,4,5};
//accum(&num[0],&num[4]);
typedef Accum <char> test;
test testabc;
const char testchar[8] = "abcdefg";
// testabc(&testchar[0],&testchar[7]);
char testa = 'a';
char testb = 'b';
const char * ptesta = &testa;
const char * ptestb = &testb;
//testabc(ptesta,ptestb);
return 0;
}
----------------------------------------------
----------------------------------------------
I got this program on net. but the testabc(ptesta,ptestb) function could not be used correctly,