读书人

C++ traints usage,该如何解决

发布时间: 2013-07-08 14:13:00 作者: rapoo

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,


the error message is "../trait.cpp:77:23: error: no match for call to ‘(test {aka Accum<const char>}) (const char*&, const char*&)’
"

how i can use this function, Could anyone check it for me ,thank you?
[解决办法]
test testabc ×
accum ○

Accum <char> does not have a operator(), you can't call testabc(ptesta,ptestb);

读书人网 >C++

热点推荐