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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T05:21:33+00:00 2026-05-14T05:21:33+00:00

I’m trying to solve a problem that anonymous functions make much, much easier, and

  • 0

I’m trying to solve a problem that anonymous functions make much, much easier, and was wondering if this was possible in c++.

What I would like to do is (essentially)

template<typename T>
T DoSomething(T one, function<T(T)> dosomething)
{
    return one + dosomething(5);
}

void GetMyVal(...)
{
   DoSomething<int>(1, /*anonymous func here*/)
}

This example is very, very simplified for what I have to do. In C# I would do p => p*5. I know this is easy with C++0x, but I can’t use that. I feel that I should be able to do it with either boost::lambda, or a compination of boost::bind and boost::function with placeholders, but I can’t seem to get it to work. This may not be possible and thats also fine, but please answer if its not possible. Thanks.

EDIT:
Ok, it seems the simple case of an int works fine, what about a more complicated structure? So, lets try

struct NumHolder
{
  int x;
}

template<typename T>
T DoSomething(T one, function<T(NumHolder)> dosomething)
{
    NumHolder temp;
    temp = 5
    return one + dosomething(temp);
}

void GetMyVal(...)
{
   DoSomething<int>(1, /*anonymous func here*/)
}

Here my C# expression would be along the lines of p => p.temp * 5. Is this possible to do in C++ with boost?

EDIT 2: OK, now I’m just curious 😀 How would I call a function within the lambda expression? So, if we have

int ChangeVal(int mult)
{
    return mult*5;
}

struct NumHolder
{
  int x;
}

template<typename T>
T DoSomething(T one, function<T(NumHolder)> dosomething)
{
    NumHolder temp;
    temp = 5
    return one + dosomething(temp);
}

void GetMyVal(...)
{
   DoSomething<int>(1, /*anonymous func here*/)
}

In C# I could call p => ChangeVal(p). What would the syntax be for this with the C++ lambda expressions?

  • 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-14T05:21:33+00:00Added an answer on May 14, 2026 at 5:21 am

    As Anders notes in his answer, boost::lambda can be useful, but the code can become hard to read in some cases. It thus depends on what you want to do in your anonymous function.

    For simple case like the p => p * 5 you mention in your question, it seems to me that using Lambda or Bind would be reasonable, though:

    DoSomething(1, _1 * 5);
    

    Edit:
    Your second example hits one area where the syntax gets quickly verbose: Member (data or function) access. Because the “dot” operator can’t be overloaded in C++, you have to use a bind expression to get the “x” from the argument:

    DoSomething(1, bind(&NumHolder::x, _1) * 5);
    

    or, with Boost.Lambda, use the overloaded ->* operator:

    DoSomething(1, &_1->* &NumHolder::x * 5);
    

    Edit 2:
    OK, one last time 🙂
    In your last question, you write that in C#, you’d write p => ChangeVal(p), but the code above shows a ChangeVal taking an int, not a NumHolder, so it’s not clear what you mean.

    Assuming that ChangeVal takes an int and that you want the anonymous function to do the equivalent of ChangeVal(the_arg.x), you’d write this with Boost.Lambda:

    DoSomething(1, bind(&ChangeVal, &_1->*&NumHolder::x));
    

    or this with Boost.Bind (works with Lambda too):

    DoSomething(1, bind(&ChangeVal, bind(&NumHolder::x, _1));
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I would like to count the length of a string with PHP. The string
For some reason, after submitting a string like this Jack’s Spindle from a text
I've got a string that has curly quotes in it. I'd like to replace
I am trying to render a haml file in a javascript response like so:
I would like to run a str_replace or preg_replace which looks for certain words
I know there's a lot of other questions out there that deal with this
I'm trying to create an if statement in PHP that prevents a single post

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.