News for hackers. Stuff that matters.
Re:C++0x (upcoming C++ standard, includes lambda functions) by chc on Wednesday August 11, 2010 @04:15PM (#1595940)
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++?
Re:C++0x (upcoming C++ standard, includes lambda functions) by bff on Wednesday August 11, 2010 @04:25PM (#1595975)
I'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.
Re:C++0x (upcoming C++ standard, includes lambda functions) by okmjuhb on Wednesday August 11, 2010 @04:56PM (#1596092)
To 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.
Re:C++0x (upcoming C++ standard, includes lambda functions) by bff on Wednesday August 11, 2010 @05:02PM (#1596109)
Return 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.
Re:C++0x (upcoming C++ standard, includes lambda functions) by okmjuhb on Wednesday August 11, 2010 @05:22PM (#1596189)
The 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.

Re:C++0x (upcoming C++ standard, includes lambda functions) by plorkyeran on Wednesday August 11, 2010 @04:30PM (#1595998)
Requiring 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.

Re:C++0x (upcoming C++ standard, includes lambda functions) by makmanalp on Wednesday August 11, 2010 @05:33PM (#1596223)
I 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.
Re:C++0x (upcoming C++ standard, includes lambda functions) by aidenn0 on Wednesday August 11, 2010 @07:32PM (#1596590)
There'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
Re:C++0x (upcoming C++ standard, includes lambda functions) by InclinedPlane on Wednesday August 11, 2010 @09:37PM (#1596869)
I 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.