#include <iostream>
#include <string>
#include <vector>
#include <array>
#include <iterator>
template<typename Iter>
auto accu(Iter begin, Iter end) {
using ValueType = typename std::iterator_traits<Iter>::value_type;
ValueType total{};
while(begin != end) {
total += *begin;
++begin;
}
return total;
}
void Test(){
std::vector<int> iArr{1,2,3,4};
std::cout<<accu(iArr.begin(),iArr.end())<<std::endl;
std::string str="abc";
short res = static_cast<short>(accu(str.begin(),str.end()));
std::cout<<accu(str.begin(),str.end())<<":"<<res<<std::endl;
}
template<typename T>
struct ActualT;
template<>
struct ActualT<char>{
using ActType = short;
static constexpr ActType zero(){
return 0;
}
};
template<>
struct ActualT<short>{
using ActType = int;
static constexpr ActType zero(){
return 0;
}
};
template<>
struct ActualT<int>{
using ActType = long;
static constexpr ActType zero(){
return 0;
}
};
template<>
struct ActualT<unsigned int>{
using ActType = unsigned long;
static constexpr ActType zero(){
return 0;
}
};
template<>
struct ActualT<float>{
using ActType = double;
static constexpr ActType zero(){
return 0;
}
};
template<typename T, typename AT=ActualT<T>>
auto Add(T* begin, T* end) {
typename AT::ActType total = AT::zero();
while(*begin != *end) {
total += *begin;
begin++;
}
return total;
}
void Test1(){
std::string str="cba";
char* p = const_cast<char*>(str.c_str());
std::cout<<Add(p,p+str.length())<<std::endl;
}
int main()
{
Test();
Test1();
}