0

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?

Community
  • 1
  • 1
behzad.nouri
  • 74,723
  • 18
  • 126
  • 124

1 Answers1

1

There are two problems with this I think:

  1. ptr_fun requires that its argument return a value. See: http://www.sgi.com/tech/stl/ptr_fun.html

  2. bind2nd doesn't work with reference arguments. See: Using std::bind2nd with references

Moral of the story: boost::bind hides 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:

int trim2(std::string s, const std::locale loc)
{
  boost::trim<std::string>(s, loc);
  return 0;
}

And then do:

std::for_each(df.colnames.begin(), df.colnames.end(),
    std::bind2nd(std::ptr_fun(trim2), std::locale()));

P.S: (1) may be library-dependent. I just tried with g++ and it didn't have a problem with a void return.

Community
  • 1
  • 1
Soverman
  • 1,135
  • 1
  • 9
  • 16