Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

> Closures are examples of powerful things which C++ simply cannot do.

Not true anymore. This article seems rather old, considering it refers to Racket as "PLT Scheme" here: http://cs.gmu.edu/~sean/lisp/, so it's at least 7 years old, possibly older.



Closures in the functional programming sense don't really exist in C++. "Closing over" a value means something quite specific, and C++ captures don't do this; they merely copy by value or reference an outside entity. The only way to get close to it is if your C++ lambdas exclusively capture and thus increment the reference count of shared pointers only, and no other types. Not exactly idiomatic.


I don't think this is correct. I'm not sure there are many people that would argue that C++11 doesn't have lexical closures just because its closures need to take into account the semantics of C++ itself. Anyone who says that, doesn't really understand how C++ is used, frankly.

You said it yourself, if you specify generic close by value, you will get the behavior you want. If you know your closure won't outlive the stuff it closes over, you can capture by ref. If you need some other behavior, you use smart pointers and capture by value or mix and match based on what you're capturing.

Just because C++ doesn't automatically figure it out for you isn't an argument against it having closures, any more than not a having bignum or rational built in means it can't do arithmetic.


It's better to call them what they are in C++, lambdas, rather than closures, in my opinion. The flexibility that proper closures provide in functional languages just cannot exist in C++ and mixing the terms freely only confuses the matter.


From https://web.archive.org/web/20170215233309/http://cs.gmu.edu... it looks like it dates from 2003 but was significantly extended last summer.


Yep, it's old. I've updated it here or there but it needs a serious revision.


Maybe a repository and pull requests could be a way to facilitate it. If there was another trusted party with commit rights it would not be so much on you.

Or not.


It is old. I remember Prof. Luke sending it to us when I took his class in Fall 2011.




Consider applying for YC's Summer 2026 batch! Applications are open till May 4

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: