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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T15:56:42+00:00 2026-05-23T15:56:42+00:00

I know what functors are and when to use them with std algorithms, but

  • 0

I know what functors are and when to use them with std algorithms, but I haven’t understood what Stroustrup says about them in the C++11 FAQ.

Can anyone explain what std::bind and std::function are, when they should be used, and give some examples for newbies?

  • 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-23T15:56:43+00:00Added an answer on May 23, 2026 at 3:56 pm

    std::bind is for partial function application.

    That is, suppose you have a function object f which takes 3 arguments:

    f(a,b,c);
    

    You want a new function object which only takes two arguments, defined as:

    g(a,b) := f(a, 4, b);
    

    g is a “partial application” of the function f: the middle argument has already been specified, and there are two left to go.

    You can use std::bind to get g:

    auto g = bind(f, _1, 4, _2);
    

    This is more concise than actually writing a functor class to do it.

    There are further examples in the article you link to. You generally use it when you need to pass a functor to some algorithm. You have a function or functor that almost does the job you want, but is more configurable (i.e. has more parameters) than the algorithm uses. So you bind arguments to some of the parameters, and leave the rest for the algorithm to fill in:

    // raise every value in vec to the power of 7
    std::transform(vec.begin(), vec.end(), some_output, std::bind(std::pow, _1, 7));
    

    Here, pow takes two parameters and can raise to any power, but all we care about is raising to the power of 7.

    As an occasional use that isn’t partial function application, bind can also re-order the arguments to a function:

    auto memcpy_with_the_parameters_in_the_right_flipping_order = bind(memcpy, _2, _1, _3);
    

    I don’t recommend using it just because you don’t like the API, but it has potential practical uses for example because:

    not2(bind(less<T>, _2, _1));
    

    is a less-than-or-equal function (assuming a total order, blah blah). This example normally isn’t necessary since there already is a std::less_equal (it uses the <= operator rather than <, so if they aren’t consistent then you might need this, and you might also need to visit the author of the class with a cluestick). It’s the sort of transformation that comes up if you’re using a functional style of programming, though.

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

Sidebar

Related Questions

Learn You a Haskell has an example about functors. I can read LYAH, and
I know you can use a javascript to do this <script type=text/javascript> touchMove =
I already know the basics about functors, how they are created and so on.
I know about that we can add facts dynamic at run time, mean fact1(+First,+Second).
I know I'm messing up the syntax somewhere but I can't figure out where.
know nothing about php, but I have this script that reads a folder and
Basically I want to do this: Can I use a lambda function or std::function
Know this might be rather basic, but I been trying to figure out how
Anyone know if it's possible to databind the ScaleX and ScaleY of a render
I know that I can do something like $int = (int)99; //(int) has 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.