#include <iostream>
#include <cstdarg>
#include <boost/type_traits.hpp>

using namespace std;

// for pod
template<typename Type>
Type sum(size_t n, ...) {
    typedef typename boost::promote<Type>::type PromotedType;
    va_list pa;
    Type sum = static_cast<PromotedType>(0);
    va_start(pa, n);
    while(n--)
        sum += va_arg(pa, PromotedType);
    va_end(pa);
    return static_cast<Type>(sum);
}

int main() {
    cout << "correct:"   << sum<unsigned> (2, 1, 2) << endl;
    cout << "error  :"   << sum<int>   (2, 1.0, 2.0) << endl;
    cout << "correct:"   << sum<double> (2, 1.0, 2.0) << endl;
    cout << "error  :"   << sum<float> (2, 1.0f, 2.0f) << endl;
    return 0;
}
