# Unity "Modern C++" auto and lambda

This topic is 1048 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

According to Microsoft, using the auto keyword, as well as lambdas can make "cleaner, tighter and easier to read"(abbreviated of course) code. I am not in any industry, but merely a lowly hobbyist. So can someone explain to me the general reasons why this is whats being pushed by Microsoft and if it is being adopted by others, why I am wrong in my disagreement with these statements?

If you remove the standard c++ library, are lambdas not merely just a "fancy" way to scope a block of code? Are anonymous functions really THAT COOL?

And how is auto easier to read? You are implying and hiding the type unless you explicitly and physically look at what it is received by?

Not starting flame war, rather an educational opportunity for my self and others.

Edited by ExErvus

##### Share on other sites

If you remove the standard c++ library

why would you do that?

Are anonymous functions really THAT COOL?

Yes. The same thing can be achieved with functors, but lambdas are an order of magnitude less verbose.

And how is auto easier to read?

Templates.

std::vector<std::string, SomeCustomAllocator> strings;

// which is easier to read
for (std::vector<std::string, SomeCustomAllocator>::iterator itr = strings.begin(); // etc

// or
for (auto itr = strings.begin(); // etc


Sure you can solve this problem with typedefs, but does

for (StringVecIterator itr = strings.begin(); //


really add any useful information to the code as you read it?

edit: damn... ninja'd!

Edited by ChaosEngine

##### Share on other sites

Sure you can solve this problem with typedefs, but does

for (StringVecIterator itr = strings.begin(); //

really add any useful information to the code as you read it?

Especially since some IDEs will tell you the deduced type if you just mouse over the variable.

##### Share on other sites

According to Microsoft, using the auto keyword, as well as lambdas can make "cleaner, tighter and easier to read"(abbreviated of course) code. I am not in any industry, but merely a lowly hobbyist. So can someone explain to me the general reasons why this is whats being pushed by Microsoft and if it is being adopted by others, why I am wrong in my disagreement with these statements?

If you remove the standard c++ library, are lambdas not merely just a "fancy" way to scope a block of code? Are anonymous functions really THAT COOL?

And how is auto easier to read? You are implying and hiding the type unless you explicitly and physically look at what it is received by?

Not starting flame war, rather an educational opportunity for my self and others.

On the large Microsoft really wants to empower a large number of software engineers through higher level technology. One might view this whole idea as a poor one when considering this is sort of the purpose of the old .NET stuff. Come ten years later we have two major ways to write code that MS enjoys: C# and the newer more abstraction heavy "modern" C++. This might just be a newer generation of engineers not learning from old mistakes, or it might be that the new high level facilities really can empower tons of engineers to do be more productive.

However there's another argument against the whole "empower the many" option: empower just a few. More experience older game industry vets usually heavily align with the idea that it's more important to have an extremely strong/experienced developer as opposed to a work force of C# coders. This alignment usually revolves around ideas like abstractions have an inherent cost, writing good code is ridiculously hard, adding more people is expensive to facilitate communication, etc.

My personal opinion on auto is that it's only useful if you're using templates. So now the question might be, is it worth it to use templates? Immediately I can see this question starts to get pretty involved, and would probably require some specific examples of real work to justify one way or another. So as long as someone tried something out and shipped a product with it I would care about their opinion. Microsoft certainly ships a lot of products, but I don't really enjoy many of them, so me specifically would not really agree with their process (like auto/lambda).

Edited by Randy Gaul

##### Share on other sites

I made me whole ui using lambdas and I realised just how amazing they are. I also use them for a event/observer-listener type system. It is so much faster, neater and can all be in one place.

If my device rotates I need to change a layout accordingly.I can pretty much listen for an event in a single line:

device.RegisterForEventConfigChanged(this, [this](float width, float height) { ChangeLayout(width, height); });

The first 'this' is used as a reference so I can unregistered later easily enough. Doing that without lambdas would be so much more involved. As has been pointed out in this thread already their power comes in their ability to capture variables, in my case capturing 'this' and being able to call ChangeLayout (which is a class member method).

• ### What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

• 14
• 14
• 45
• 22
• 27
• ### Forum Statistics

• Total Topics
634044
• Total Posts
3015213
×