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 3350546
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T01:48:00+00:00 2026-05-18T01:48:00+00:00

Suppose I have a function which takes a nullary functor as an argument: void

  • 0

Suppose I have a function which takes a nullary functor as an argument:

void enqueue( boost::function<void()> & functor );

I have another function which takes an int and does something internally:

void foo( int a);

I would like to nest, but not compose, these together so that I get a functor with the signature:

boost::function<void(int)> functor

Which when called with a value – say 4 – performs the following:

enqueue( boost::bind(&foo, 4) )

My first attempt was the following:

boost::function<void(int)> functor = boost::bind(&enqueue, boost::bind(&foo,_1))

This fails because bind performs composition when given a nested bind. foo was first called, then the value void was “returned” to enqueue, which fails.

My second attempt was the following:

boost::function<void(int)> functor = boost::bind(&enqueue, boost::protect( boost::bind(&foo, _1) ) )

This failed because enqueue accepts a nullary, not unary functor.

Can what I’m seeking be done?

Other information:

  • This is basically identical to the unanswered boost forum question from 6 years ago:
    http://lists.boost.org/boost-users/2004/07/7125.php
  • Some reading suggests that using boost::lambda::bind with boost::lambda::unlambda and boost::lambda::protect may do what I’m seeking. Unfortunately boost::lambda has an unacceptably low number of allowed placeholders (3), and high compile-time overhead.
  • 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-18T01:48:00+00:00Added an answer on May 18, 2026 at 1:48 am

    Interesting question…

    What you basically want is a “bound call to bind”. In the same manner than binding a call to foo(x, y) is written bind(&foo, x, y), binding a call to bind(&foo, x) should be like bind(&bind, &foo, x). However, taking the address of an overloaded function quickly gets ugly and, as boost::bind has more overloads than I could count, it gets pretty ugly:

    // One single line, broken for "readability"
    boost::function<void(int)> f = boost::bind(
      &enqueue, 
      boost::bind(
        static_cast<
          boost::_bi::bind_t<
            void, void(*)(int), boost::_bi::list_av_1<int>::type
          >
          (*)(void(*)(int), int)
        >(&boost::bind), 
        &foo, 
        _1
      )
    );
    

    You’ll probably agree that, while “interesting”, the above won’t win readability contests. Separating the obtention of the proper bind overload from the rest makes things a bit more manageable:

    boost::_bi::bind_t<void, void(*)(int), boost::_bi::list_av_1<int>::type>
      (*bind_foo)(void(*)(int), int) = &boost::bind;
    
    boost::function<void(int)> q = boost::bind(&enqueue, boost::bind(bind_foo, &foo, _1));
    

    but I still hesitate to recommend it 😉

    Edit:

    Answering the OP’s comment about how/if C++0x would help to clean the syntax: It does:

    auto f = [](int i){enqueue([=](){foo(i);});};
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Suppose I have a function which takes variadic arguments ( ... ) or a
Suppose you have a function 'normalize' which takes a list of numbers (representing a
Suppose I have two functions which look like this: public static void myFunction1(int a,
I have a list A , and a function f which takes an item
Suppose I have a string which is a backslash-escaped version of another string. Is
Suppose I have the following code: class siteMS { ... function __CONSTRUCT() { require
Suppose I have a stringbuilder in C# that does this: StringBuilder sb = new
Suppose I have the following code: class some_class{}; some_class some_function() { return some_class(); }
I have a function I've written that was initially supposed to take a string
Of course most languages have library functions for this, but suppose I want to

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.