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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T14:08:28+00:00 2026-05-23T14:08:28+00:00

How do I create a lambda function using boost or the stl to match

  • 0

How do I create a lambda function using boost or the stl to match the boost::function parameter expected by F in the third snippet of code in main?

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

void F(int a, boost::function<bool(int)> f) {
    std::cout << "a = " << a << " f(a) = " << f(a) << std::endl;
}

bool G(int x) {
    return x == 0;
}

int main(int arg, char** argv) {
    // C++0x
    F(123, [](int i) { return i==0; } );

    // Using seperate function
    F(0, &G);

    // How can I do it in place without C++0x
    F(123, /* create a lambda here to match */);
}

I can’t use C++0x and would like to avoid creating several separate functions. I can use something other that boost::function if that helps, my priority is creating the lambda succinctly.

  • 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-23T14:08:29+00:00Added an answer on May 23, 2026 at 2:08 pm
    #include <functional>    // STL
    #include <boost/lambda/lambda.hpp>   // Boost.Lambda
    #include <boost/spirit/include/phoenix_core.hpp>     // Boost.Pheonix
    #include <boost/spirit/include/phoenix_operator.hpp> // Boost.Pheonix also
    
    ...
    
    // Use STL bind without lambdas
    F(0, std::bind2nd(std::equal_to<int>(), 0));
    F(123, std::bind2nd(std::equal_to<int>(), 0));
    
    // Use Boost.Lambda (boost::lambda::_1 is the variable)
    F(0, boost::lambda::_1 == 0);
    F(123, boost::lambda::_1 == 0);
    
    // Use Boost.Phoenix
    F(0, boost::phoenix::arg_names::arg1 == 0);
    F(123, boost::phoenix::arg_names::arg1 == 0);
    

    You may want to add some using namespace to simplify the code.

    Boost.Lambda is strictly for defining functors inline with a C++-like syntax, while Boost.Phoenix is a functional-programming language built on top of C++ abusing (☺) its syntax and compile-time computation capability. Boost.Phoenix is much more powerful than Boost.Lambda, but the former also takes much more time to compile.

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

Sidebar

Related Questions

Sorry for the confusing title. Let me explain via code: #include <string> #include <boost\function.hpp>
Is it possible to create an inline lambda using boost which always throws an
I'm using PHPs create_function($args, $code) function to dynamically load a function definition from a
I have created an ienumerable of lambda functions using this function static IEnumerable<Func<int>> MakeEnumerator(int[]
I'm using F# to create a lambda calculus. I am currently stuck trying to
I'm trying to create a function which can be called with a lambda that
I want to define the block as a string, then create the lambda. The
how can I create a dynamic lambda expression to pass to use in my
I'm trying to expect an error in an rspec test. lambda {Participant.create!({:user_id => three.id,
I want to create a list of lambda objects from a list of constants

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.