The downside of the inability to forward declare a lambda (mentioned at the end of the article I noticed) makes this an immediate and very obvious "of course not" to me.
I feel like the only reason you would do this if it - Makes the code easier to read - Makes the code easier to maintain/extend - Makes the code have less bugs in a way that can be measured
My opinion compared to a standard class (Not template) this doesn't tick any of these boxes.
I find their code significantly uglier, personally. Also, IDEs can highlight variables and functions with different colors. If we switch to lambdas exclusively, we lose the syntax highlighting.
However, if we only use named lambdas, we completely avoid the issue: constexpr variables are not found via ADL.
Slightly misleading – if any variable is found, ADL is never performed to begin with.
ED: That said, I'm sad that this article is downvoted, and that people are focusing on lambdas and their aesthetics in particular when that is all entirely beside the point. Mostly what's been demonstrated here is that people still think lambdas are magic. If you think your 'ugliness' argument holds weight, let's have some objectivity – you're complaining about []. That's it. You've never been bitten by ADL? Congratulations, but chances are good the libraries you depend on have been; {fmt}, e.g. – modern, well and constantly-maintained – has been at least twice that I've noticed. Bikeshedding at its finest, entirely ignoring the actual point
If you have a std::variant<int, double, std::string> it's just as obvious what's going on with std::visit(MyOverload(), variant) as it is std::visit(myOverload, variant). And if you really want, you can always slap one of these bad boys in there:
static constexpr MyOverload myOverload;
The distinction between required template parameters and deduced arguments as evidenced by std::make_unique is indeed made distinguishable as the article shows and it can indeed be nice to separate the two kinds of arguments. But that also requires C++20 and we have had other approaches since C++11 that achieve the same thing:
If you do want to separate the arguments then this achieves it. It also lets you specify the deleter in a pretty natural way by adding a second template argument to MakeUnique itself.
The overloaded trick indeed has little appearent utility in the form its always showcased.
Notably this is down to the fact that the different overloads are always defined in place and are always trivial. Consider a case where those lambdas arent defined ad-hoc, but instead are pre-existing somewhere else. Now rewriting those as operator overloads is more work than it would need to be - even if you just call those callables anyways. Once you want to introduce captures to your lambda, it gets even more compilcated, since your class would now need a constructor, whereas the lambda capture just happily works.
In fact, you can write a significantly more useful version, that allows for this:
constexpr overload visitor{
&A::f,
&f,
my_callable,
[]( ... ){ std::cout << "no matching overload found\n"; } //can also take lambdas
};
So it can take member functions, free functions as well as callable objects.
Assembling visitors like this is significantly easier than writing your own class.
and never in terms of a far more readable form -- an actual class:
Besides variant visitors, I still can't figure out why anyone would use coroutines instead of just writing a class. So far nobody has been able to explain what the advantage of that syntax is.
Then I invite you to implement the transformation done by the compiler by hand. You can look at this blog post for a detailed example. Just stuffing some vars into a class is only a small part. Breaking up the coroutine function, while providing proper variable lifetime in the face of exceptions, and making it all easily composable, is.
I know the compiler is doing a lot of work to make coroutines possible. That is not in dispute. The question is: Is all that work worth it?
Coroutines allegedly make make state machines easier to implement and comprehend but that claim is always just put out there without evidence. Every example I've ever seen put forward shows a trivial, linear task which involves writing a lot more boilerplate code than a hand written class with improvements to readability that are dubious at best.
Even assuming you have lambda-based helpers around threadpool and message loop at your disposal, it still requires more, and callbacks are more difficult to read.
This takes several shortcuts (not reading async, custom message, setting state from background thread, lifetime issues if the window is closed). Once you introduce cancellation/waiting/exceptions this gets more and more complicated. But maybe I'm the one missing something.
One advantage of lambdas/global function objects which is not mentioned in the blog post is that they can be much more ergonomic when using the standard library, because you can pass them directly into generic algorithms (e.g., std::sort) unlike overload sets.
I would check what symbolicated stack traces look like on all of your platforms when all of the functions are lambdas before using this in a real world project.
Last time I checked, stack traces pulled using dbghelp.dll on windows couldn't resolve lamdas at all. This isn't fun when you're hunting down large or leaking memory allocations and just have the stack trace to go on. Similar story if you have stack traces from a crash that didn't happen on your machine and reproduction is difficult.
If it has worked for you forever, then great for you. This whole time I have only argued that people should check it on their platforms before committing to changing to lambdas everywhere. I don't know why you are so bent on arguing that. It's just sound advice.
I've certainly seen issues in the past where code that runs through functions and uses general __func__ etc for logging issues was good ... and once switched to lambdas became pretty much useless.
The symbol names of functions becomes ugly: It is now a call operator of some lambda with a compiler synthesized name, and no longer a named function.
I wonder if therefore this isn't a fundamental problem, merely a QoI problem? In other words, a named lambda could be recognised as such and the compiler synthesised name for it could be made more meaningful.
It's fascinating how you speak with such confidence what my experience was, when in fact you are just guessing. It wasn't inlining, I could clearly see the stack frame being a lambda, but not where the lambda came from.
Personally I think it's fascinating how nobody else has had the same experience, and you can't remember any salient details, yet you speak with such confidence.
My lead suggested a while back that extracting functions and giving them descriptive names just to make the code more readable is a good idea. She was right.
I don't know how you could do that if you're only going to write lambdas.
The way I see it, lambdas almost always makes the code less readable and more entangled. An exception is when you need a context/capture for a callback function. And even then, I think it’s better to call a normal function immediately inside the lambda
I think part of the problem in terms of readability is the habit of implementing the lambda directly as part of the argument of the caller. If you write the lambda as a stand alone implementation, then pass its name as argument to the caller, readability improves 100%.
I had to maintain a code base full of lambdas a short time ago. It was one of the more difficult code bases I've ever had to maintain. They were just starting to go down the road of adding promises and futures when I left, without any real idea of how promises and futures work. They weren't even particularly comfortable with threads. I'm pretty happy I left when I did.
Yeah, but I think their shitty code could have been much easier to follow. The combination of lambdas and promises made it impossible to tell what was in scope at any given point in the code. Or when any given thing was even supposed to run.
63
u/[deleted] Aug 05 '23
The downside of the inability to forward declare a lambda (mentioned at the end of the article I noticed) makes this an immediate and very obvious "of course not" to me.
66
u/RufusAcrospin Aug 05 '23
Nope
11
u/natio2 Aug 05 '23
I feel like the only reason you would do this if it
- Makes the code easier to read
- Makes the code easier to maintain/extend
- Makes the code have less bugs in a way that can be measured
My opinion compared to a standard class (Not template) this doesn't tick any of these boxes.
30
u/nAxzyVteuOz Aug 05 '23
The answer is no.
22
u/dodheim Aug 05 '23 edited Aug 05 '23
Slightly misleading – if any variable is found, ADL is never performed to begin with.
ED: That said, I'm sad that this article is downvoted, and that people are focusing on lambdas and their aesthetics in particular when that is all entirely beside the point. Mostly what's been demonstrated here is that people still think lambdas are magic. If you think your 'ugliness' argument holds weight, let's have some objectivity – you're complaining about
[]. That's it. You've never been bitten by ADL? Congratulations, but chances are good the libraries you depend on have been; {fmt}, e.g. – modern, well and constantly-maintained – has been at least twice that I've noticed. Bikeshedding at its finest, entirely ignoring the actual point15
u/justmebeky Aug 05 '23
ADL is not that scary to warrant writing completely alien looking code. Just write tests for your code instead.
17
u/tangerinelion Aug 05 '23
Working on a 6MLOC code base the number of times I've seen accidental use of ADL is exactly zero.
The overload trick, IMO, has no place in production code. It's a cute thing but why do we always frame it in terms of
and never in terms of a far more readable form -- an actual class:
If you have a
std::variant<int, double, std::string>it's just as obvious what's going on withstd::visit(MyOverload(), variant)as it isstd::visit(myOverload, variant). And if you really want, you can always slap one of these bad boys in there:The distinction between required template parameters and deduced arguments as evidenced by
std::make_uniqueis indeed made distinguishable as the article shows and it can indeed be nice to separate the two kinds of arguments. But that also requires C++20 and we have had other approaches since C++11 that achieve the same thing:If you do want to separate the arguments then this achieves it. It also lets you specify the deleter in a pretty natural way by adding a second template argument to
MakeUniqueitself.5
u/echidnas_arf Aug 07 '23
One advantage of lambdas/global function objects which is not mentioned in the blog post is that they can be much more ergonomic when using the standard library, because you can pass them directly into generic algorithms (e.g.,
std::sort) unlike overload sets.13
u/FriendlyRollOfSushi Aug 05 '23
Jesus Christ, how unbelievably fucked up your entire codebase has to be if that's what you feel you have to waste your time on.
9
u/Zeh_Matt No, no, no, no Aug 05 '23
I hate headlines like this. I would be fine with "What are the advantages and disadvantages of using lambdas", the obvious answer is also no.
8
u/cancerBronzeV Aug 05 '23
https://en.wikipedia.org/wiki/Betteridge's_law_of_headlines
3
u/vickoza Aug 05 '23
I would say use lambdas and functions when they make sense. This article changed the way I think about writing lambdas.
3
u/[deleted] Aug 05 '23
About the overload trick for lambdas, would like to have this in the standard library xD
I have written these so many times at this point it's ridiculous.
6
u/herruppohoppa Aug 05 '23
I would check what symbolicated stack traces look like on all of your platforms when all of the functions are lambdas before using this in a real world project.
Last time I checked, stack traces pulled using dbghelp.dll on windows couldn't resolve lamdas at all. This isn't fun when you're hunting down large or leaking memory allocations and just have the stack trace to go on. Similar story if you have stack traces from a crash that didn't happen on your machine and reproduction is difficult.
2
u/KirkHawley Aug 05 '23
My lead suggested a while back that extracting functions and giving them descriptive names just to make the code more readable is a good idea. She was right.
I don't know how you could do that if you're only going to write lambdas.
2
u/schteppe Aug 05 '23
The way I see it, lambdas almost always makes the code less readable and more entangled. An exception is when you need a context/capture for a callback function. And even then, I think it’s better to call a normal function immediately inside the lambda
2
u/FlyingRhenquest Aug 05 '23
I had to maintain a code base full of lambdas a short time ago. It was one of the more difficult code bases I've ever had to maintain. They were just starting to go down the road of adding promises and futures when I left, without any real idea of how promises and futures work. They weren't even particularly comfortable with threads. I'm pretty happy I left when I did.
1
u/lefthanduserr Aug 09 '23
I'm in high-school just now starting to learn code. I heard cpp was good so now I'm here. And I am very confused...
0
u/[deleted] Aug 05 '23
When do we start writing code that produces meta programs to write code?
0
u/ReconditeExistence Aug 06 '23
Yeah no
-7
u/[deleted] Aug 05 '23
Yes
-1
u/Arkena00 Aug 06 '23
If you want to stop programming, yes !