accum1.h
#ifndef ACCUM_HPP
#define ACCUM_HPP
template<typename T>
inline T accum(T const* beg, T const* end)
{
T total = T();
while (beg != end)
{
total += *beg;
++beg;
}
return total;
}
#endif
test_accum1.cpp
#include "accum1.hpp"
#include <iostream>
using namespace std;
int main()
{
int num[] = {1, 3, 5, 7, 9};
cout << "the average result: "
<< accum<int>(&num[0], &num[5]) / 5
<< endl;
char name[] = "template";
int len = sizeof(name) - 1;
cout << "the average result: "
<< accum<char>(&name[0], &name[len]) / len
<< endl;
return 0;
}