Re: How to pass a binary_function functor as an argument?
In article <1175020906.822418.69400@l77g2000hsb.googlegroups.com>,
<t_littell@yahoo.com> wrote:
I want to pass a binary_function<double, double, double> functor
object as an argument into another function. So, I tried the
following which does not work with std::accumulate():
std::binary_function<...> does not make a class polymorphic in the
sense that it knows what the derived class is. std::binary_function<...>
is a convience struct containing three typedefs nothing else.
The simplest solution is to make the functor a template parameter
and pass it by value or const reference.
template <class Functor>
double foo(std:::vector<double> &v,double init,Functor f)
{
return std::accumulate(v.begin(),v.end(),init,f):
}
if you don't want a template use boost::function<...> or
tr1::function<...> to hold the functor, then foo is not a template
function
double foo(std::vector<double>const &v,double init,
boost::function<double(double,double)> f);
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]