Article View: comp.lang.c++
Article #1021Re: A better experimental::scope_exit
From: Tristan Wibberley
Date: Tue, 24 Nov 2026 12:42
Date: Tue, 24 Nov 2026 12:42
121 lines
7561 bytes
7561 bytes
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.
--
Tristan Wibberley
The message body is Copyright (C) 2026 Tristan Wibberley except
citations and quotations noted. All Rights Reserved except that you may,
of course, cite it academically giving credit to me, distribute it
verbatim as part of a usenet system or its archives, and use it to
promote my greatness and general superiority without misrepresentation
of my opinions other than my opinion of my greatness and general
superiority which you _may_ misrepresent. You definitely MAY NOT train
any production AI system with it but you may train experimental AI that
will only be used for evaluation of the AI methods it implements.
Technical Details
Message-ID:
<11e40mr$2qdb1$1@dont-email.me>Path:
mbox-mo1.pugleaf.net!srv.newsdeef.eu!news.hispagatos.org!eternal-september.org!feeder.eternal-september.org!nntp.eternal-september.org!.POSTED!not-for-mailReferences:
<11dsk17$8ske$2@raubtier-asyl.eternal-september.org> <11e2kpl$2cc7p$1@dont-email.me> <11e39t7$2ia0c$1@raubtier-asyl.eternal-september.org>Show Full Headers
Complete Article Headers
Path: mbox-mo1.pugleaf.net!srv.newsdeef.eu!news.hispagatos.org!eternal-september.org!feeder.eternal-september.org!nntp.eternal-september.org!.POSTED!not-for-mail From: Tristan Wibberley <tristan.wibberley+netnews2@alumni.manchester.ac.uk> Newsgroups: comp.lang.c++ Subject: Re: A better experimental::scope_exit Date: Tue, 24 Nov 2026 12:42:02 +0000 Organization: A noiseless patient Spider Lines: 121 Message-ID: <11e40mr$2qdb1$1@dont-email.me> References: <11dsk17$8ske$2@raubtier-asyl.eternal-september.org> <11e2kpl$2cc7p$1@dont-email.me> <11e39t7$2ia0c$1@raubtier-asyl.eternal-september.org> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Injection-Date: Tue, 24 Nov 2026 12:42:04 +0000 (UTC) Injection-Info: dont-email.me; logging-data="2962785"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/+sW3VPq/g3mqrGfNWCjOpXEmiXE9vIUJzym/XYZFWTw=="; posting-host="decbdae56b93c9f0180ef0a73d46de56" User-Agent: Mozilla Thunderbird Cancel-Lock: sha1:5/Z+88c6bWpvouugittdKMkSoJ8= sha256:XuZ9YdwQnAHo+Be5GhmLfttcmoorbKdkXLNeQufTQaU= sha1:QaXGl6xDvPNsdiptYTkxXfldLCY= sha256:Xsq0ARo9X2tAtRQ0Q6UMwfFgyVsooZikb6DDLkRT6S0= In-Reply-To: <11e39t7$2ia0c$1@raubtier-asyl.eternal-september.org> Content-Language: en-GB