While trying to get comfortable with boost
, stumbled into problem with using boost::function
along with std::vector
. I'm trying to do a simple thing: have a list of functions with similair signatures and then use all that functions with std::for_each
on sample data. Here is the code:
typedef boost::function<int (const char*)> text_processor;
typedef std::vector<text_processor> text_processors;
text_processors processors;
processors.push_back(std::atoi);
processors.push_back(std::strlen);
const char data[] = "123";
std::for_each(processors.begin(), processors.end(),
std::cout << boost::bind(&text_processors::value_type::operator(), _1, data)
<< "\n"
);
So, with for_each
I'm trying to write to standard output the result of applying every function to sample data. But it won't compile like this (some long message about missing operator <<
for bind
result).
If I remove stream operators then I'll have compilable, but useless code. The trick is that I want to do function applying and text output in single for_each
. What am I missing? Thought it should be easy with lambdas or smth like that, but can't figure out the correct solution.