1

when using closure in other languages, it just feels nature, variables from outer scope are captured automatically , without the need of declaring such captures.

in c++11, good to see we have closures, but why they make it so uncomfortable to work with, not only have to declaring the capture, but also read write controls?

not likely implementation considerations, but what kind of philosophy is leading to this awkward spec ?

  • 7
    It's the same philosophy it's always had: "Allow the user to choose whatever behaviour fits their particular needs, then make the language comfortable to use if possible." Having to declare capturing behaviour is akin to having to declare that a method may be overridden, because someone, somewhere might want a method that is not dynamically dispatched. – Kilian Foth Nov 26 '13 at 7:06
  • 5
    Secondly, note that "capturing variables" in C++ isn't trivial. You might want to capture something by value, or by reference. If you capture something by reference and it exists on the stack, that's fine until your closure rises above that stack frame in which case it isn't fine, but if you capture by value then you can't modify the original at all. You can't pick one as a "standard" action, therefore you must be able to pick both, therefore unfortunately c++'s closure syntax becomes a little more verbose. – Phoshi Nov 26 '13 at 9:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.