TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

Scope exit in C++11 (2012)

39 pointsby nochover 7 years ago

8 comments

mehrdadnover 7 years ago
What everybody seems to miss (including in the other comments here) is the fact that no kind of hackery with destructors will ever make them equivalent to scope(exit) or finally{}, because of the simple reason that destructors invoked via exceptions can&#x27;t throw their own exceptions. And this makes sense, because destructors are meant to <i>release resources</i>, whereas finally&#x2F;scope(exit) are meant to <i>execute after prior operations in a LIFO manner</i>. Not all operations that need to occur in a LIFO manner are inherently tied to resource acquisition. More fundamentally, one is inherently about data, and the other is inherently about control-flow... simply put, they&#x27;re different tools with different use cases, even though people don&#x27;t bother to make the distinction. Nevertheless, if people insist on using the same hammer for both nails, the language needs to address this fundamental issue before people can claim it&#x27;s possible to do scope(exit) or the equivalent in C++.
评论 #16321511 未加载
评论 #16320824 未加载
评论 #16320765 未加载
评论 #16321882 未加载
评论 #16324582 未加载
评论 #16321336 未加载
lyricaljokeover 7 years ago
This type of construct is unnecessary in C++11; the presence of RAII containers and structures totally obviates it. C++ provides a language-level construct for logic performed at the end of object lifetime (i.e., when value types fall out of scope or when heap-allocated objects are deleted): the destructor!<p>This person is describing an error-prone reimplementation of std::unique_ptr with a custom deleter. I could understand the value of an article like this if the use of C++11 were not allowed, but given that the article is written with C++11 in mind, I fear the author has totally missed the point of RAII-style resource management that the newer language-level constructs provide.<p>Deterministic destruction and associated logic, as well as the RAII strategies that follow, are perhaps the greatest strength of C++. Languages like Rust have gone on to improve this even further by eliminating some of the greatest sources of error. Capabilities like this don&#x27;t seem accurately described as C++ learning from D; indeed, if D provides hooks for running logic when names fall out of scope, that seems more accurately described as vice versa.
评论 #16320865 未加载
jordighover 7 years ago
It&#x27;s nice to see some D ideas make it into C++.<p>I think that D is kind of like Mercurial: both less popular than some other wildly popular alternative, but we still hope that at least some of their ideas trickle back into the more popular sibling.
评论 #16320178 未加载
评论 #16321883 未加载
评论 #16320241 未加载
评论 #16320108 未加载
favoritedover 7 years ago
This is pretty similar to std::lock_guard (and similar RAII types). Except instead of unlocking (or tearing down a resource, etc.) upon destruction, it invokes a lambda.
jwilkover 7 years ago
The site doesn&#x27;t load for me. Here&#x27;s an archived copy:<p><a href="https:&#x2F;&#x2F;archive.is&#x2F;srQvw" rel="nofollow">https:&#x2F;&#x2F;archive.is&#x2F;srQvw</a>
laytheaover 7 years ago
Wow, so much debate over the specific manner in which to solve a problem. More needed on the which problems to solve, I think.
EgoIncarnateover 7 years ago
(2012)
smitherfieldover 7 years ago
You can do the same thing with no C++11 features other than C99 variadic macros. (IMO cleaner, too).<p><pre><code> #define SCOPE_EXIT(...)\ struct SE##__LINE__##_{~SE##__LINE__##_(){__VA_ARGS__;}}se##__LINE__##_</code></pre>
评论 #16321387 未加载
评论 #16321788 未加载
评论 #16320646 未加载
评论 #16320753 未加载