4 ms·
So the lambda functions are implemented in a way that only really works with templates and you have to explicitly pass scope? I don't really write C++ "natively
by chc 16y ago
So the lambda functions are implemented in a way that only really works with templates and you have to explicitly pass scope? I don't really write C++ "natively," but this seems awkward to me, particularly vis-a-vis Apple's implementation of blocks in C. Does it look better to anyone who has a better handle on the feel of idiomatic C++?
- bff 16y agoI've been using them for a while now (gcc has support) and they're not awkward. The ability to specify scope is actually rather nice and most of the time you'll either leave the scope empty with [] or pass everything in by value [=] or reference [&]. The return type of the lambda function is also usually detectable by the compiler so you only need to specify it rarely.
- okmjuhb 16y agoTo be more specific: the return type of the lambda can be omitted for a lambda whose body is just "return {expression}". The compiler could, if it wanted, always deduce the return type (since implementing type inference would be easy). The fact that it doesn't isn't to make it easy for compilers; it was a design decision of the language: having unspecified return types for complicated pieces of code hurts readability and maintainability.
- bff 16y agoReturn types can't always be deduced since they can be ambiguous - imagine an if statement where one branch returns a float and the other returns a double. The desired return type could really be any numeric value since the types can be converted so the return type does need to be specified. Since C++ has overloaded functions the compiler can't just look at the function that the result is being passed into to determine the type either.
- okmjuhb 16y agoThe solution would be then to do autoconversions as appropriate or return an error. The compiler already deals with this issue with the ?: operator. It's a readability and maintenance problem there, too. It'd be easy to do; the language designers chose not to.
- plorkyeran 16y agoRequiring the use of templates (or std::function) is how closures work in C++ already. The new lambda syntax is just a (much) easier way to create closures, so it suffers from some of the same problems. I've never found wrapping things in std::function to be much of a problem, though. You can "explicitly" pass all variables in the containing scope with [=] or [&], so it's more of an optional annotation than a requirement.
- makmanalp 16y agoI guess by making the closure explicit they've made implementing them much easier too. The problem of trying to figure out which variables the function is closed over is no longer there (which makes me curious because judging from my own experience in creating a scheme-clone, this wasn't that difficult). I bet it just transforms down into a function or maybe even gets inlined.
- aidenn0 16y agoThere's no such thing as idiomatic C++. The language is so huge and complex that all successful teams I've worked with that use it have selected a subset to use as part of their coding standard. In each case those subsets were different
- InclinedPlane 16y agoI wish I could upvote this +10. Most languages have a spectrum of skill, and it generally is possible to progress upwards in skill for any given language as long as you have mastered the basics. C++ is different. Because C++ is very much: "here's some tools to write your own C-like language, go nuts!" Idiomatic C++ is so widely variant it rightly justifies as being different languages. The extreme end of the spectrum of C++ usage (and I speak not of niche usage but day-in-day-out being-used-at-huge-companies usage) is as different from "ordinary" idiomatic C++ as Python or Ruby might be.