#include"accum0.h"
#include<iostream>int main()
{int num[] = {1,2,3,4,5};char name[] = "templates";int length = sizeof(name)-1;std::cout << "The average value of the integer values is :"<< accum(&num[0],&num[5])/5 << std::endl;std::cout << "The average value of the characters in \" "<< name << "\" is: "<< accum(&name[0],&name[length])/length<< std::endl;return 0;
}
?
代碼產生的結果為: The average value of the integer values is :3 The average value of the characters in “templates” is :-5
//accumtraits2.h
template<typename T>
class AccumulationTraits;template<>
class AccumulationTraits<char>{
public:typedef int AccT;
};template<>
class AccumulationTraits<short>{
public:typedef int AccT;
};template<>
class AccumulationTraits<int>{
public:typedef long AccT;
};template<>
class AccumulationTraits<unsigned int>{
public:typedef unsigned long AccT;
};template<>
class AccumulationTraits<float>{
public:typedef double AccT;
};