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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T18:16:54+00:00 2026-05-22T18:16:54+00:00

I REALLY love lambdas and having the ability to use them in C++ is

  • 0

I REALLY love lambdas and having the ability to use them in C++ is a pleasure. But, as I’m used to Haskell, where lambdas fit really well into the syntax, I’m struggling with how to use them in C++ without writing unreadable cluttered long code lines.

So, as an example, suppose I’d write this:

vector<double> foo(10,0.2);
for_each(foo.begin(), foo.end(), [](double x){ std::cout << x << " ";})

this is not so difficult to read, the lambda expression is pretty small. But if I have a two or three line long function inside that for_each, this could become a problem for my
code-reading-skills:

vector<double> foo(10,0.2);
randomNumberGenerator bar;
for_each(foo.begin(), foo.end(), [](double x){ std::cout << "hello!"; x+=bar()/(1+bar()); std::cout << x << " ";})
//sorry, I couldn't think of a less stupid example... 

This line is starting to get annoyingly long and difficult to read for my taste…

What is your preferred code conventions for this case? Should I write:

for_each(foo.begin(), foo.end(), 
          [] (double x) {
                std::cout << "hello!"
                x += bar()/(1+bar());
                std::cout << x << " ";
          });

or something like it? I still think this syntax feels a bit unnatural and difficult to read… 🙁

  • 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-22T18:16:55+00:00Added an answer on May 22, 2026 at 6:16 pm

    I usually go for

    for_each(foo.begin(), foo.end(), [](double x) {
        std::cout << "hello!"
        x += bar()/(1+bar());
        std::cout << x << " ";
    });
    

    I’ve written some several hundred line lambdas.

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

Sidebar

Related Questions

I'm pretty new to scala and I really love it so far but I'm
I can't stand XCode, but really love OpenFrameworks, and I know it works on
I'm fairly new (ok, REALLy new) to generics but I love the idea of
I would really love some help with this. Currently when I use the following
First, I'd like to say that I really love NoSQL & MongoDB but I've
I'd really love to use Mersenne Twister as alternative to arc4random. So to make
I really love node.js but it´s really complicating when you want to run multiple
I really love using the IEnumerable extension methods. But sometimes I end up with
Everyone's raved about how awesome Qt is and I would really love to use
I use the jQuery UI .autocomplete() , which I really love. I do have

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.