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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T18:55:58+00:00 2026-05-30T18:55:58+00:00

What advantages are there to using lambda over using a #define macro? I’ve encountered

  • 0

What advantages are there to using lambda over using a #define macro?

I’ve encountered some situations where I’ve used lambda for convenience, but I could have easily used a similar define macro to solve the problem in the same way.

Example (Random code):

auto dyndelay=[=]()->bool{return(mode?(queue.front().initTime+delay)<GetTickCount():TRUE);};

Has no benefit over:

#define dyndelay (mode?(queue.front().initTime+delay)<GetTickCount():TRUE)

@Oli Charlesworth

#define dyndelay ....;
#define adifferentlambda ....;
#define myDefine(x) x:dyndelay:adifferentroutine
bool someCondition;

myDefine(someCondition)
  • 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-30T18:56:00+00:00Added an answer on May 30, 2026 at 6:56 pm

    You’re looking at things the completely wrong way. A lambda is a much more general construct. It models anonymous functions and a form of closure. It can even be captured into a common form using function objects which can then be used at runtime to model a form of indirect function call (similar to function pointers or virtual functions).

    A macro is just a brute force text substitution mechanism.

    However, it appears like you are comparing the two on a syntactical basis. Yes, if you want to achieve the shortest syntax possible, macros are the way to go. They’re also the way to go to ruin your code. In fact, I can even reduce hello world, using macros, to something like this:

    INCLUDE_EVERYTHING
    MAIN
    PRINT_HELLO_WORLD
    END_BLOCK
    

    We can invent whole new languages this way using the preprocessor! Of course we also won’t be able to effectively trace through the code, we throw away a lot of the usefulness of C++ (scoping), and no one else will ever want to read or work with our code. But hey, the syntax is shorter…

    Where you’ll find no macro substitution will ever work is when you start involving generic algorithms which is one of the most obvious places for lambdas to be used.

    vector<string> v = {...};
    sort(begin(v), end(v), [](const string& str1, const string& str2){
        return str1.size() < str2.size();
    });
    // v is sorted based on string length starting from shortest string to longest
    

    Of course, you could still go out of your way to create a bloated, crippled version of all the generic algorithms where everything is implemented as a macro and then you can work with just macros.

    Next you can try to do things like implement a signals and slots mechanism using only macros, another place where lambdas are useful.

    In summary, I think you need to understand why the preprocessor is discouraged so strongly in C++. That’s a more general subject that goes into scoping, debugging, confusing errors (as a replace of sheer text substitution), etc. As GMan pointed out, to try to compare the two based on syntax alone suggests you lack fundamental understanding of both macros and lambdas and what purposes they are supposed to serve.

    In fairness though, you can create a shorter syntax using macros. After all, a copy/paste-style text substitution mechanism will let you get away with a lot of shortcuts. But be very aware of the problems of doing that. As you get more comfortable with C++, you’ll find yourself accepting some syntactical overhead in order to avoid macros when you start working in production code and seeing the problems that the preprocessor can cause firsthand. As you start getting more comfortable with things that involve function objects like generic algorithms (including parallel_for) and signals and slots, you’ll also begin to appreciate the true power of lambdas much more.

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

Sidebar

Related Questions

Are there any advantages of using one over the other besides readability?
There are all sorts of advantages to using Emacs, but for someone comfortable with
There are unarguably lots of advantages in using dvcs and it can be used
Are there any advantages in using Digest::SHA over Digest::SHA1 or vice versa? both seem
Are there any advantages of using wildcard-type generics in the Bar class over completely
When isolating code for a release, is there any advantages to using labels over
When developing for Alfresco, are there any advantages to using the MMT over Integrated
What advantages are there for using either XSLT or Linq to XML for HTML
Can anyone explain what advantages there are to using a tool like MSBuild (or
Regarding separations of concerns only, are there advantages of using ASP.NET MVC instead of

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.