Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6628479
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T22:08:10+00:00 2026-05-25T22:08:10+00:00

While trying to get comfortable with boost , stumbled into problem with using boost::function

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T22:08:10+00:00Added an answer on May 25, 2026 at 10:08 pm

    The problem with your code is that you are trying to create a functor in place in a way that is not allowed (you cannot just throw code at the third argument of for_each, you need to pass a functor).

    Without lambda support in the compiler you can use std::transform rather than std::for_each (not tested… but this should work):

    std::transform( processors.begin(), processors.end(),
                    std::ostream_iterator<int>( std::cout, "\n" ),
                    bind( &text_processors::value_type::operator(), _1, data ) );
    

    If your compiler supports lambdas you can do with it:

    const char data[] = "123";
    std::for_each(processors.begin(), processors.end(),
        [&data]( text_processors const & ) { 
            std::cout << boost::bind(&text_processors::value_type::operator(), _1, data) 
                  << "\n"
        }
    );
    

    But then you can avoid the bind altogether:

    std::for_each( processors.begin(), processors.end(),
                   [&data]( text_processors::value_type & op ) {
                       std::cout << op( data ) << "\n";
                   }
    );
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

While trying to GET a JSON, my callback function is NOT firing. $.ajax({ type:GET,
I am encountering a problem with string formatting while trying to get only the
I've been trying to get into F# on and off for a while but
I'm trying to get familiar with CI and I have run into a problem
I followed these instructions while trying to get remote debugging working with Delphi 2007.
While trying to do: perl -I'/v1/data/site_perl' -MCPAN -e 'install Log::Dispatch'; I continue to get
Trying to get an ASP application deployed; it worked for a while but then
I am trying to get my query to grab multiple rows while returning the
While I trying to debug a 'C' program with gdb, I always get the
I've been trying to get started with unit-testing while working on a little cli

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.