0

Today I was doing some catch-up on c++11 (as we have not moved on yet). One of the reasons to switch as stated by a lot of people seems to be lambda expressions. I am still unsure on how they offer something new.

For instance using c++11:

#include <iostream>

int main()
{
    auto func = [] () { std::cout << "Hello world" << std::endl; };

    func();
}

seems to be very similar to:

#include <iostream>

#define FUNC( )\
        do { std::cout << "Hello world" << std::endl; } while(0)


int main()
{
    FUNC();
}

What would lambda expressions offer me that I can't currently already do?

12
  • 1
    You can't use your version in place where a comparator is required, for example. Also macros are C relict. Commented Jan 22, 2013 at 15:05
  • 8
    you're seriously comparing a lambda function with a macro? Commented Jan 22, 2013 at 15:05
  • No, I said that to me it looks like they give the same result and I was merely asking a question as to what the difference is. I don't quite understand why this needs to be downvoted or be compared to "trained dolphin attackers". Commented Jan 22, 2013 at 15:19
  • I didn't downvote. Your question is perfectly valid. Macros are trained dolphin attackers. They are the worst tool imaginable for almost any purpose. I'm not suggesting that you or the question are trained dolphin attackers. Commented Jan 22, 2013 at 15:21
  • @Lieuwe Well I didn't downvote, I was just puzzled by the question. Macros are evil. They are a text replacement tool only. No error checking possible whatsoever. lambdas are proper functions. The compiler actually understands them. Commented Jan 22, 2013 at 15:21

2 Answers 2

6

http://msdn.microsoft.com/en-us/library/vstudio/dd293608.aspx sums up the main points and more on the subject in great detail. Here is the salient excerpt:

A lambda combines the benefits of function pointers and function objects and avoids their disadvantages. Like a function objects, a lambda is flexible and can maintain state, but unlike a function object, its compact syntax doesn't require a class definition. By using lambdas, you can write code that's less cumbersome and less prone to errors than the code for an equivalent function object.

There are examples on the site showing more differences and comparisons.

Also...conventional wisdom is never use macros in C++:

http://scienceblogs.com/goodmath/2007/12/17/macros-why-theyre-evil/

Sign up to request clarification or add additional context in comments.

Comments

1

A less obvious benefit of lambdas when used with the standard algorithms is that the programmer is not required to think of a name for the lambda function, and naming things is considered to be a difficult task in programming.

Additionally, the code executed via the standard algorithms is often used to invoke a member function(s) on each object in the supplied range, with the name of the functor, or function, often just parroting the name of the member function being invoked and adding nothing to readability of the code. Contrived example:

struct object
{
    void execute() const {}
};

void run_execute(object const& o) { o.execute(); }

std::vector<object> v;

std::for_each(v.begin(), v.end(), run_execute);

std::for_each(v.begin(), v.end(), [](object const& o) { o.execute(); });

Admittedly this is a minor benefit but still pleasant.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.