Re:Lambda expression comparison between C++11, C++14 and C++17
by csl on Sunday May 15, 2016 @05:35AM
(#11700151)
I find that pointlessly pedantic. By the same measure, languages that don't offer bignums don't offer integers. Even Lisps need to implement closures in one way or another, and you may be surprised to see how they actually do it.
Re:Lambda expression comparison between C++11, C++14 and C++17
by moonshinefe on Sunday May 15, 2016 @05:53AM
(#11700196)
I think you miss the point, the OP was mentioning the property that in most languages, closures whose outer variables they are bringing into scope stay in scope (even if the outer function ends). In C++ they expire (is what I got from his comment, anyway).
Re:Lambda expression comparison between C++11, C++14 and C++17
by gpderetta on Sunday May 15, 2016 @08:06AM
(#11700604)
Closed-by-value variables (I.e. the default) in C++ don't expire. Referenced or pointed-to object might, but this is completely consistent with the rest of the language.
Remember that C++ is a by value language. Pointers are explicit, fist class and distinct from the pointed to object.
References are werid, though.
Re:Lambda expression comparison between C++11, C++14 and C++17
by makecheck on Sunday May 15, 2016 @11:02AM
(#11701154)
You would have to combine copying and reference management, e.g. "[=]" and std::shared_ptr<>. It definitely requires the programmer to pay more attention though, compared to other languages/constructs.
Re:Lambda expression comparison between C++11, C++14 and C++17
by leni536 on Sunday May 15, 2016 @04:00AM
(#11699985)
I'm not familiar with closures from other languages. What about capturing a shared_ptr by value?
Re:Lambda expression comparison between C++11, C++14 and C++17
by thrownaway2424 on Sunday May 15, 2016 @11:38AM
(#11701293)
The thing to keep in mind is that copying a shared_ptr isn't cheap at all. It's a class with a pointer and atomic reference count inside and the atomic inc/dec takes many cycles.
Re:Lambda expression comparison between C++11, C++14 and C++17
by leni536 on Sunday May 15, 2016 @01:47PM
(#11701893)
How does this compare to the cost of a closure in other languages? Yeah atomic reference counts are not cheap, but basically that's the point of a shared_ptr.
Re:Lambda expression comparison between C++11, C++14 and C++17
by thrownaway2424 on Sunday May 15, 2016 @05:27PM
(#11702856)
I'm not sure. I don't program in C++ because I want it to have performance comparable to other languages.
If we talk only about c++ capture lists by value (i.e. [=]), then you could make a case for a more appropriate use of the word "closure" but since many lambdas do more than this, I think the distinction is necessary.
However, even in by-value captures, if you are capturing a pointer by value, the issue remains. So, really it is not a good idea to think of lambdas as closures in the functional sense typically used in other languages.