I'm trying to implement a standard deviation routine using templates but I hit a snag with the bind2nd operator and I just don't get it:
Code:
template<typename T> struct minus_squared
: public std::binary_function<T, T, T> {
T operator()(const T& t1, const T& t2) const {
return pow(t1 - t2, 2);
}
};
template<typename S, template <typename T> class Container>
double sd(const Container<S>& ct) {
double m = mean(ct);
return sqrt(std::accumulate(ct.begin(), ct.end(), 0.0,
std::bind2nd(minus_squared<S>(), m))
/ (ct.size() - 1));
}
I'm calling it with a vector of doubles and the error I get is:
Code:
/usr/include/c++/4.0.0/bits/stl_numeric.h: In function ‘_Tp std::accumulate(_InputIterator, _InputIterator, _Tp, _BinaryOperation) [with _InputIterator = __gnu_cxx::__normal_iterator<const double*, std::vector<double, std::allocator<double> > >, _Tp = double, _BinaryOperation = std::binder2nd<tlin::minus_squared<double> >]’:
named_matrix.h:145: instantiated from ‘double tlin::sd(const Container<S>&) [with S = double, Container = std::vector]’
schone.cpp:14: instantiated from here
/usr/include/c++/4.0.0/bits/stl_numeric.h:116: error: no match for call to ‘(std::binder2nd<tlin::minus_squared<double> >) (double&, const double&)’
/usr/include/c++/4.0.0/bits/stl_function.h:439: note: candidates are: typename _Operation::result_type std::binder2nd<_Operation>::operator()(const typename _Operation::first_argument_type&) const [with _Operation = tlin::minus_squared<double>]
/usr/include/c++/4.0.0/bits/stl_function.h:445: note: typename _Operation::result_type std::binder2nd<_Operation>::operator()(typename _Operation::first_argument_type&) const [with _Operation = tlin::minus_squared<double>]
meaning obviously that "binder2nd" is expecting only one argument but is getting two arguments. What I don't understand is why?