Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>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).</p> <p>A macro is just a brute force text substitution mechanism.</p> <p>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:</p> <pre><code>INCLUDE_EVERYTHING MAIN PRINT_HELLO_WORLD END_BLOCK </code></pre> <p>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...</p> <p>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.</p> <pre><code>vector&lt;string&gt; v = {...}; sort(begin(v), end(v), [](const string&amp; str1, const string&amp; str2){ return str1.size() &lt; str2.size(); }); // v is sorted based on string length starting from shortest string to longest </code></pre> <p>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.</p> <p>Next you can try to do things like implement a signals and slots mechanism using only macros, another place where lambdas are useful.</p> <p>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.</p> <p>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.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload