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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T09:11:35+00:00 2026-05-13T09:11:35+00:00

I have a question about which style is preferred: std::bind Vs lambda in C++0x.

  • 0

I have a question about which style is preferred: std::bind Vs lambda in C++0x. I know that they serve -somehow- different purposes but lets take an example of intersecting functionality.

Using lambda:

uniform_int<> distribution(1, 6);
mt19937 engine;
// lambda style
auto dice = [&]() { return distribution(engine); };

Using bind:

uniform_int<> distribution(1, 6);
mt19937 engine;
// bind style
auto dice = bind(distribution, engine);

Which one should we prefer? why? assuming more complex situations compared to the mentioned example. i.e. What are the advantages/disadvantages of one over the other?

  • 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-13T09:11:35+00:00Added an answer on May 13, 2026 at 9:11 am

    As you said, bind and lambdas don’t quite exactly aim at the same goal.

    For instance, for using and composing STL algorithms, lambdas are clear winners, IMHO.

    To illustrate, I remember a really funny answer, here on stack overflow, where someone asked for ideas of hex magic numbers, (like 0xDEADBEEF, 0xCAFEBABE, 0xDEADDEAD etc.) and was told that if he were a real C++ programmer he would simply have download a list of English words and use a simple one-liner of C++ 🙂

    #include <iterator>
    #include <string>
    #include <algorithm>
    #include <iostream>
    #include <fstream>
    #include <boost/lambda/lambda.hpp>
    #include <boost/lambda/bind.hpp>
    
    int main()
    {
        using namespace boost::lambda;
        std::ifstream ifs("wordsEn.txt");
        std::remove_copy_if(
            std::istream_iterator<std::string>(ifs),
            std::istream_iterator<std::string>(),
            std::ostream_iterator<std::string>(std::cout, "\n"),
            bind(&std::string::size, _1) != 8u
                ||
            bind(
                static_cast<std::string::size_type (std::string::*)(const char*, std::string::size_type) const>(
                    &std::string::find_first_not_of
                ),
                _1,
                "abcdef",
                0u
            ) != std::string::npos
        );
    }
    

    This snippet, in pure C++98, open the English words file, scan each word and print only those of length 8 with ‘a’, ‘b’, ‘c’, ‘d’, ‘e’ or ‘f’ letters.

    Now, turn on C++0X and lambda :

    #include <iterator>
    #include <string>
    #include <algorithm>
    #include <iostream>
    #include <fstream>
    
    int main()
    {
     std::ifstream ifs("wordsEn.txt");
     std::copy_if(
        std::istream_iterator<std::string>(ifs),
        std::istream_iterator<std::string>(),
        std::ostream_iterator<std::string>(std::cout, "\n"),
        [](const std::string& s)
        {
           return (s.size() == 8 && 
                   s.find_first_not_of("abcdef") == std::string::npos);
        }
     );
    }
    

    This is still a bit heavy to read (mainly because of the istream_iterator business), but a lot simpler than the bind version 🙂

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

Sidebar

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.