🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.lang.c++
Article #1024

Re: A better experimental::scope_exit

#1024
From: Ross Finlayson
Date: Tue, 24 Nov 2026 16:59
164 lines
9706 bytes
On 11/24/2026 04:42 AM, Tristan Wibberley wrote:
> On 24/11/2026 06:12, Bonita Montero wrote:
>> Am 24.11.2026 um 01:12 schrieb Tristan Wibberley:
>>
>>>     The facility is here eliciting the feeling that, by using it, one may
>>> disregard the possibility that exceptions pass out of it's members and
>>> destructor or that exceptions passing out of the callbacks I provide to
>>> it are unable to affect its behaviours that I intuitively expect.
>>>     However, only Fn is tested for being nothrow invocable. There are also
>>> ::run and ::disable of the dependency node to consider.
>>
>> My template trickery checks if the dependent node is a
>> defer<>; so it's also checked if it's fn is also noexcept.
>
> Oh! I didn't notice that.
>
>
>>>     There is much to talk about on interface design so this would be
>>> longwinded and annoying if I did. I wonder if it's better to work with
>>> client callbacks that throw exceptions so the surprises of the limited
>>> compose-ability are removed. I suppose it depends on the scale of the
>>> development that the class is being used in.
>>
>> I thought about that for a while and I came to the conclusion that this
>> scope guard isn't different from a normal destructor, which is noexcept
>> by default; so I did it the same way to enforce Fn to be noexcept.
>>
>>> Does C++ still allow noexcept(false) destructors and does an exception
>>> at the end of a destructor allow the members to be destroyed? ...
>> Yes, you can do that; but I guess that's very uncommon.
>
> Of course it wasn't so until C++14 when the preset mode was changed from
> noexcept(false) to noexcept(true) (was it C++14?), and before C++11
> there wasn't even a way to change it /from/ noexcept(false). So I think
> it's really common. A unix workstation multi-process application
> exhibits the most ordinary behaviours around success, failure, and
> cleanup when relying on that feature (which probably explains why it was
> the only easy way to do things in C++ until C++11 and was the default
> until C++14.
>
> There are also core aspects of meaning about trying to rollback/reset
> and throwing if you can't rollback/reset and then aborting if you can't
> reset something. Here's the original concept at least from C++98 to
> C++11 (default) and C++14 (nondefault but available).
>
> You can:
>   a) during primary success:
>      try to release resource (scope guarding) and:
>        1) succeed, or
>        2) fail with exception which unwinds (invariants maintained)
>             (standard scope guarding - throws from destructor)
>   b) during primary failure (unwinding with exception in flight):
>      try to release resource (scope guarding) and:
>        1) succeed, unwinding of the primary failure continues, or
>        2) secondary failure with exception which aborts because an
>              primary exception is already in flight
>              (invariants not maintained)
>
> Those are how it was set up to work until C++11, and is obviously
> correct for the limited case where one wants to abort in (b.2) which is
> typical in multiprocess unix applications because invariants are not
> maintained - so continuation is necessarily invalid. When one does not
> make a problem with secondary exceptions having the same type or
> ancestor type as primary exceptions in (a.2) - the dangers in this area
> might have been a motivation for adding noexcept(true) in C++11 and
> defaulting to noexcept(true) in C++14. For most ordinary scope guard
> classes one does not make such a problem.
>
> C++11 added the ability to change (b.2) so that an exception could be
> nested if a destructor threw when it was being run during unwinding
> instead of aborting. Then you handle the failure-to-handle-a-failure -
> invariants /are/ maintained in general by conditions, but the immediate
> surrounding sequence is for a tighter unconditional invariant so you
> unwind that bit only and catch with the looser invariants of a degraded
> mode. I haven't made good use of that but I think it's done wrong and
> ought to have been done via insertion of exception translation functions
> into the handler-search phase because relying on the use of try and
> catch inside scope guards to translate exceptions makes a lot of new
> problems vs. callbacks called at the point of the original "throw".
>
> We now have nested exceptions so we can structure code as the above with
> the below (a,b,c.i) and add in (c.ii) when (c.i) is invalid in some
> special places like O/S executives where scope guards maintain
> invariants over shared factors. However, I don't know how it goes when
> you go and compose larger programs using the same pattern arbitrarily
> deeply because I haven't used the feature.
>
>
> a) assumed events:
>   - flow-through
> b) assumed events during reset from detection of unassumed event:
>   - destroy objects during unwinding
> c) assumed events during reset from detection of unassumed event
>     during reset from detection of unassumed event:
>   - either:
>     i)  throw during unwinding destruction and therefrom terminate
>         instead of unwinding further, or
>     ii) throw during unwinding destruction storing the original
>         exception as nested exception
>
> (a)    - everyone knows and uses it
> (b)    - very popular (scope guards rely on it)
> (c.i)  - some people spend a lot of effort avoiding it and making
>           programs understandable only in tunnel focus
>           In fact, it is merely (b) with the ability to add on "and if
>           handling further errors gracefully is too hard, just abort
>           like everyone else does"
> (c.ii) - I never got round to looking close enough at it.
>
>


Thanks for writing, an account of noexcept in the signature vis-a-vis
nodiscard as attribute about the signature and the exception-signature,
has it so that in scope-managed like block-scoped and function-scoped
resource management, that exceptions are a true way in the language
to give the guaranteed behavior of propagation of errors from deeper
than the shallow in the usual account of a function-stack.


That the industry has leaned away from exceptions since untrusted
or 3'rd party code, or, bugs, otherwise would result trashing the
stack, or "goto isn't good or bad, it just is", then considered
to patch that back together with nodiscard prodding discipline,
doesn't take it away from the language that exception-handling
the stack-unwinding is already well-defined.


The account of exception-translation, for example about asynchronous
code which is basically covariant and about "generic" or "in-specific"
or "wrapped" exceptions vis-a-vis that's just the error code and
accounts of, for example, "structured errors", and since "structured
exception handling", in the system and the language, has the usual
account that code that swallows exceptions that aren't specifically
modeled after error-modeling and exception-modeling as well-defined,
is undefined behavior.


Java and C++ have a lot of sympatico, basically since Java's inception
and "hide all the pointers, everything's a pointer, we'll refcount
the pointers", then for class theory and type theory, for example
about C++'s "multiple inheritance" account vis-a-vis Java's "single
inheritance" account then with Java's interfaces cum C++'s concepts,
of course these things have been observed by anybody familiar with
the development and maturations of these languages on their systems
their eco-systems.


Accounts of ownership and responsibility basically make for distinctions
in types that any "move" is a "copy", that it's
sort of a helper and dodgy what lives up-the-stack vis-a-vis
what lives down-the-stack, which shows itself in the _holes_
of "ref ref" or "&&" where pointers have multiple purposes
and should basically have own types, about "quantifier disambiguation"
then "pointer disambiguation" which is these days that "universal"
or, what was that, "forwarding references" or "ref ref", is
a bit the bitter pill the syntactic sugar.






Technical Details
Message-ID: <t_adnbNl1dzyX5j2nZ2dnZfqn_SdnZ2d@giganews.com>
Path: mbox-mo1.pugleaf.net!srv.newsdeef.eu!weretis.net!feeder9.news.weretis.net!border-3.nntp.ord.giganews.com!nntp.giganews.com!local-1.nntp.ord.giganews.com!Xl.tags.giganews.com!local-2.nntp.ord.giganews.com!news.giganews.com.POSTED!not-for-mail
References: <11dsk17$8ske$2@raubtier-asyl.eternal-september.org> <11e2kpl$2cc7p$1@dont-email.me> <11e39t7$2ia0c$1@raubtier-asyl.eternal-september.org> <11e40mr$2qdb1$1@dont-email.me>

Show Full Headers
Complete Article Headers
Path: mbox-mo1.pugleaf.net!srv.newsdeef.eu!weretis.net!feeder9.news.weretis.net!border-3.nntp.ord.giganews.com!nntp.giganews.com!local-1.nntp.ord.giganews.com!Xl.tags.giganews.com!local-2.nntp.ord.giganews.com!news.giganews.com.POSTED!not-for-mail
NNTP-Posting-Date: Tue, 24 Nov 2026 16:59:27 +0000
Subject: Re: A better experimental::scope_exit
Newsgroups: comp.lang.c++
References: <11dsk17$8ske$2@raubtier-asyl.eternal-september.org>
 <11e2kpl$2cc7p$1@dont-email.me>
 <11e39t7$2ia0c$1@raubtier-asyl.eternal-september.org>
 <11e40mr$2qdb1$1@dont-email.me>
From: Ross Finlayson <ross.a.finlayson@gmail.com>
Date: Tue, 24 Nov 2026 08:59:12 -0800
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:38.0) Gecko/20100101
 Thunderbird/38.6.0
MIME-Version: 1.0
In-Reply-To: <11e40mr$2qdb1$1@dont-email.me>
Content-Type: text/plain; charset=utf-8; format=flowed
Content-Transfer-Encoding: 7bit
Message-ID: <t_adnbNl1dzyX5j2nZ2dnZfqn_SdnZ2d@giganews.com>
Lines: 164
X-Usenet-Provider: http://www.giganews.com
X-Trace: sv3-SWeYHSE0CZyjo/0V3DHGPn/KGrVlWyURgh4CCQaDFL4vVR5+s9nUfGPv8sPzXxfznbdR/zkEMvdergs!BVryci2Roja111VGMaQCAmXHPHmCNN9dSMzCdoUlIhcwa4Gnguf4lV6uvAsC8bednDssFPmhFEqS
X-Complaints-To: abuse@giganews.com
X-DMCA-Notifications: http://www.giganews.com/info/dmca.html
X-Abuse-and-DMCA-Info: Please be sure to forward a copy of ALL headers
X-Abuse-and-DMCA-Info: Otherwise we will be unable to process your complaint properly
X-Postfilter: 1.3.40