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

  • SEARCH
  • Home
  • 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 6551669
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T12:21:58+00:00 2026-05-25T12:21:58+00:00

Using boost-bind , the resulting boost-function may receive more arguments than the bound object

  • 0

Using boost-bind, the resulting boost-function may receive more arguments than the bound object expects. Conceptually:

int func() { return 42; }
boost::function<int (int,int,int)> boundFunc = boost::bind(&func);
int answer = boundFunc(1,2,3);

In this case, func() receives 1,2, and 3 on the stack even though its signature indicates that it takes no arguments.

This differs from the more typical use of boost::bind for partial application, where values are fixed for certain objects yielding a boost::function that takes fewer arguments but supplies the correct number of arguments when invoking the bound object.

The following code works with both MSVC++2010 SP1. This is a reduced form to post; the original code also works under g++4.4 on Linux.

Is the following well-defined according to the C++ Standard?

#include <iostream>
#include <boost/bind.hpp>
#include <boost/function.hpp>

using namespace std;

void func1(int x) { std::cout << "func1(" << x << ")\n"; } // end func1()

void func0() { std::cout << "func0()\n"; } // end func0()

int main(int argc,char* argv[]) 
{
        typedef boost::function<void (int)> OneArgFunc;
        OneArgFunc oneArg = boost::bind(&func1,_1);
        // here we bind a function that accepts no arguments
        OneArgFunc zeroArg = boost::bind(&func0);
        oneArg(42);
        // here we invoke a function that takes no arguments 
        // with an argument. 
        zeroArg(42);

   return 0;
} // end main()

I understand why zeroArg(42) works: the unused argument is put on the stack by the calling routine and simply not accessed by the called routine. When the called routine returns, the calling routine cleans up the stack. Since it put the arguments on the stack, it knows how to remove them.

Will moving to another architecture or c++ compiler break this? Will more aggressive optimization break this?


I’m looking for a stronger statement, either from the Boost documentation or from the Standards document. I haven’t been able to find an unambiguous position in either.

Using a debugger and looking at the assembly and stack, it is clear that func from the first example does not receive values 1,2, and 3: you are correct on that front. The same is true for func0 in the second example. This is true at least for the implementations that I am looking at, MSVC++2010SP1 and g++4.4/Linux.

Looking at the referenced Boost documentation, it’s not as clear as I would like that it is safe to pass extra arguments:

bind(f, _2, _1)(x, y);                 // f(y, x)

bind(g, _1, 9, _1)(x);                 // g(x, 9, x)

bind(g, _3, _3, _3)(x, y, z);          // g(z, z, z)

bind(g, _1, _1, _1)(x, y, z);          // g(x, x, x)

Note that, in the last example, the function object produced by bind(g, _1, _1, _1) does not contain references to any arguments beyond the first, but it can still be used with more than one argument. Any extra arguments are silently ignored, just like the first and the second argument are ignored in the third example. [Emphasis mine.]

The statement about extra arguments being ignored is not as unambiguous as I would like to convince me that this is true in the general case. Looking at TR1, it is clear from Sec 3.6.3 that a callable object return from bind can be called with a different number of arguments than the target object expects. Is that the best guarantee available?

  • 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-25T12:21:58+00:00Added an answer on May 25, 2026 at 12:21 pm

    Yes, this is safe and portable – as mentioned explicitly in the documentation, the bind-expression returned by boost::bind silently ignores extra arguments.

    I.e., in your first example, func does not receive the values 1, 2, and 3 – boundFunc receives the values 1, 2, and 3 and forwards them to the contained bind-expression, which safely receives and ignores them, and then invokes func(). Likewise, in your second example, zeroArg receives the value 42 and forwards it to the contained bind-expression, which receives and ignores the value and then calls func0().

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using boost (signals + bind) and c++ for passing function reference. Here is
I am trying to use boost::bind with a boost::function using this. It seems a
I'm using boost::asio, and I have code like this: void CServer::Start(int port) { tcp::acceptor
I'm new in using boost and have a problem. I need shared_mutex function in
#include <boost/bind.hpp> #include <iostream> using namespace std; using boost::bind; class A { public: void
#include <cstdlib> #include <iostream> #include <boost/bind.hpp> #include <boost/asio.hpp> using boost::asio::ip::tcp; class session { public:
Suppose I have a function which takes some form of predicate: void Foo( boost::function<bool(int,int,int)>
#include <iostream> #include <set> #include <algorithm> #include <boost/lambda/lambda.hpp> #include <boost/bind.hpp> using namespace std; using
As the title might convey, I'm having problems seeing how Boost Bind, Boost Function,
I am creating a new boost::thread using boost::bind , and storing it in a

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.