9

Running a lambda on scope exit seems like such a basic thing, I would expect it to be standardized. Things like unique_ptr are better, when they apply, but I find there is an endless supply of "one-off" destructors are needed, especially when leveraging C-style libraries. Does anyone know if this is coming?

Yakk - Adam Nevraumont
  • 262,606
  • 27
  • 330
  • 524
VoidStar
  • 5,241
  • 1
  • 31
  • 45
  • In which cases are you using lambdas on scope exit? – Borgleader May 22 '15 at 20:21
  • Waiting for thread pool work items to complete before closing the thread pool is a good example (even when an exception is thrown you still have to wait or it will crash the workers). It's always possible to create a full C++ wrapper for each thing you do, but its so much extra code in the cases where you use it one time. – VoidStar May 22 '15 at 20:27
  • Write a proposal? I happen to like [Stefanus Du Toit](https://github.com/CppCon/CppCon2014/blob/master/Presentations/C%2B%2B11%20in%20the%20Wild%20-%20Techniques%20from%20a%20Real%20Codebase/auto.h)'s implementation (though I have no idea why he called it `Auto`) – Barry May 22 '15 at 20:34
  • @Barry: That is actually Arthur O'Dwyer's implementation - see https://goo.gl/0t8bXR – sdt Aug 03 '15 at 02:46
  • @sdt Oops. Reading comprehension fail on my part. Though comment remains about `Auto` :) – Barry Aug 03 '15 at 11:22

2 Answers2

10

n4189 is a proposal to add make_scope_exit wrappers, and other similar resource handlers, to the language. It is based off of the relatively famous scope_guard talk.

The most recent "current paper status" from LWG is in 2013, prior to the above date.

The contents of C++1z (hopefully C++17) are yet to be determined.

C++1z status for clang does not mention it. C++1z TS for clang does not mention it.

The paper itself contains an example implementation. I do not know what licensing terms it is under.

Yakk - Adam Nevraumont
  • 262,606
  • 27
  • 330
  • 524
  • 3
    It [seems to have passed LEWG](https://issues.isocpp.org/show_bug.cgi?id=6), but looks like it will need better wording before it can be adopted. N4189's wording is pretty bad. – T.C. May 23 '15 at 01:46
10

It would appear that the current version of the scope exit paper, P0052, will be going into the Library Fundamentals v3, for likely adoption in the Post-C++17 standard.

In short, not gonna happen for C++17. Sorry.

Nicol Bolas
  • 449,505
  • 63
  • 781
  • 982