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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T19:57:27+00:00 2026-06-08T19:57:27+00:00

Possible Duplicate: std::bind a bound function void foo0(int val) { std::cout << val <<

  • 0

Possible Duplicate:
std::bind a bound function

void foo0(int val) { std::cout << "val " << val << "\n"; }
void foo1(int val, std::function<void (int)> ftor) { ftor(val); }
void foo2(int val, std::function<void (int)> ftor) { ftor(val); }

int main(int argc, char* argv[]) {
    auto                applyWithFoo0       ( std::bind(foo0,     std::placeholders::_1) );
    //std::function<void (int)> applyWithFoo0       ( std::bind(foo0,     std::placeholders::_1) ); // use this instead to make compile
    auto                applyFoo1       (     std::bind(foo1, std::placeholders::_1, applyWithFoo0) );
    foo2(123, applyFoo1);
}

The sample above does not compile giving multiple errors like: Error 1 error C2780: '_Ret std::tr1::_Callable_fun<_Ty,_Indirect>::_ApplyX(_Arg0 &&,_Arg1 &&,_Arg2 &&,_Arg3 &&,_Arg4 &&,_Arg5 &&,_Arg6 &&,_Arg7 &&,_Arg8 &&,_Arg9 &&) const' : expects 10 arguments - 2 provided.
Using the commented line with explicit type does compile. It seems that the type inferred by auto is not correct. What is the problem with auto in this case?
Platform: MSVC 10 SP 1, GCC 4.6.1

  • 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-06-08T19:57:29+00:00Added an answer on June 8, 2026 at 7:57 pm

    The issue is that std::bind treats “bind expression” (like your applyWithFoo0) differently from other types. Instead of calling foo1 with applyWithFoo0 as parameter it tries to invoke applyWithFoo0 and pass its return value to foo1. But applyWithFoo0 doesn’t return anything that is convertible to std::function<void(int)>. The intention of handling “bind expressions” like this is to make them easily composable. In most cases you probably don’t want bind expression to be passed as function parameters but only their results. If you explicitly wrap the bind expression into a function<> object, the function<> object will simply be passed to foo1 directly since it is not a “bind expression” and therefore not handled specially by std::bind.

    Consider the following example:

    #include <iostream>
    #include <functional>
    
    int twice(int x) { return x*2; }
    
    int main()
    {
      using namespace std;
      using namespace std::placeholders;
      auto mul_by_2 = bind(twice,_1);
      auto mul_by_4 = bind(twice,mul_by_2); // #2
      auto mul_by_8 = bind(twice,mul_by_4); // #3
      cout << mul_by_8(1) << endl;
    }
    

    This actually compiles and works because instead of passing a functor to twice like you might expect from the bind expressions #2 and #3, bind actually evaluates the passed bind expressions and uses its result as function parameter for twice. Here, it is intentional. But in your case, you tripped over this behaviour by accident because you actually want bind to pass the functor itself to the function instead of its evaluated value. Wrapping the functor into a function<> object is obviously a work-around for that.

    In my opinion this design decision is a bit awkward because it introduces an irregularity people have to know about to be able to use bind correctly. Maybe, we’ll get another more satisfying work around in the future like

    auto applyFoo1 = bind( foo1, _1, noeval(applyWithFoo0) );
    

    where noeval tells bind not to evaluate the expression but to pass it directoy to the function. But maybe the other way around — explicitly telling bind to pass the result of a functor to the function — would have been a better design:

    auto mul_by_8 = bind( twice, eval(mul_by_4) );
    

    But I guess, now it’s too late for that …

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

Sidebar

Related Questions

Possible Duplicate: Floating point inaccuracy examples double a = 0.3; std::cout.precision(20); std::cout << a
Possible Duplicate: Bind Vs Lambda? My use of std::bind had dropped to 0 now
Possible Duplicate: Is std::list<>::sort stable? Does C++ std::list sort function is guaranteed to preserve
Possible Duplicate: sum of elements in a std::vector I have std::vector<int> and I want
Possible Duplicate: Convert std::string to const char* or char* void setVersion(char* buf, std::string version)
Possible Duplicate: std::vector<std::string> to char* array I have to call a c function that
Possible Duplicate: Cost of using std::map with std::string keys vs int keys? if I
Possible Duplicate: Advantages of using arrays instead of std::vector? What are the main advantages/disadvantages
Possible Duplicate: C++ std::string conversion problem on Windows How to convert std::string to LPCSTR?
Possible Duplicate: How is std::iostream buffered? This might sound ridiculous, but how can I

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.