I am trying to use boost::trim on a vector of strings. I understand that this solutions works elegantly, however I cannot understand why
std::for_each(df.colnames.begin(), df.colnames.end(),
std::bind2nd(std::ptr_fun(boost::trim<std::string>), std::locale()));
doesn’t work. I get error:
error: ‘typename _Operation::result_type std::binder2nd<_Operation>::operator()(typename _Operation::first_argument_type&) const [with _Operation = std::pointer_to_binary_function<std::basic_string<char>&, const std::locale&, void>; typename _Operation::result_type = void; typename _Operation::first_argument_type = std::basic_string<char>&]’ cannot be overloaded
Why std::bind2nd doesn’t work here?
There are two problems with this I think:
ptr_funrequires that its argument return a value. See:http://www.sgi.com/tech/stl/ptr_fun.html
bind2nddoesn’t work with reference arguments. See: Using std::bind2nd with referencesMoral of the story:
boost::bindhides a shocking amount of complexity.If you really want to make it work and don’t care about passing strings/locales by value you can wrap trim as follows:
And then do:
P.S: (1) may be library-dependent. I just tried with g++ and it didn’t have a problem with a void return.