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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T17:20:02+00:00 2026-05-26T17:20:02+00:00

int func(int x){return x;} … std::function<int(int)> x = std::bind(func, std::placeholders::_1); x(123); Does x(123) actually

  • 0
int func(int x){return x;}
...
std::function<int(int)> x = std::bind(func, std::placeholders::_1);
x(123);
  1. Does x(123) actually call the operator() of the functor which std::function generated which in turn calls the operator() of the functor which std::bind generated which finally calls func? Does this get optimized into something as optimal as calling func(123)?
  2. Where does the functor live which std::bind generates? In what scope? And how does std::bind name it? (can there be name collisions)
  3. Can lambdas replace all uses of std::bind?
  4. Is std::bind as optimal as implementing it as a lambda instead?
  5. What’s up with the syntax of the template argument of std::function? How does that get parsed and how can I use that template argument syntax elsewhere?
  • 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-26T17:20:02+00:00Added an answer on May 26, 2026 at 5:20 pm

    Does x(123) actually call the operator() of the functor which std::function generated which in turn calls the operator() of the functor which std::bind generated which finally calls func? Does this get optimized into something as optimal as calling func(123)?

    I wouldn’t describe the operator() of std::function as ‘generated’ (it’s a regular member), but otherwise that is a good description. Optimizations are up to your compiler, but be warned that to optimize the indirection of std::function (which requires the use of type erasure) a compiler may need to perform heroics.

    Where does the functor live which std::bind generates? In what scope? And how does std::bind name it? (can there be name collisions)

    The call to std::bind returns a functor of unspecified type, and a copy of that functor is stored inside the x object. This copy will live as long as x itself. There is no name involved so I’m not sure what you mean by that.

    Can lambdas replace all uses of std::bind?

    No. Consider auto bound = std::bind(functor, _1); where functor is a type with an overloaded operator(), let’s say on long and int. Then bound(0L) doesn’t have the same effect as bound(0) and you can’t replicate that with a lambda.

    Is std::bind as optimal as implementing it as a lambda instead?

    This is up to the compiler. Measure yourself.

    What’s up with the syntax of the template argument of std::function? How does that get parsed and how can I use that template argument syntax elsewhere?

    It’s a function type. Perhaps you’re already familiar with the syntax for pointers/references to functions: void(*)(), int(&)(double). Then just remove the pointer/reference out of the type, and you just have a function type: void(), int(double). You can use those like so:

    typedef int* function_type(long);
    function_type* p; // pointer to function
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Say a function takes a void pointer as an argument, like so: int func(void
The below does not compile: Func<int, int> fac = n => (n <= 1)
const int thing = 1234; int func( int hello ) { return hello /
Suppose I have a function that allocates memory for the caller: int func(void **mem1,
Is there anyway I can do this without writing func2<int>(); int func() { return
So, here is some basic code which illustrates my question: #include <functional> int func(int
#include <functional> struct A { int func(int x, int y) { return x+y; }
#include <functional> int func(int x, int y) { return x + y; } int
int* func() { int* i=new int[1]; //do something return i; } void funcc() {
Suppose we have: interface Foo { bool Func(int x); } class Bar: Foo {

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.