#pragma once
#include <type_traits>
#include <cassert>
#include <utility>
#include "nui.hpp"
template<typename Fn, typename Dependent = void>
struct defer final
{
ÿÿÿÿstatic_assert( std::is_nothrow_invocable_v<Fn> );
ÿÿÿÿdefer( Fn fn, Dependent *pDep = (Dependent *)nullptr ) :
ÿÿÿÿÿÿÿ m_enabled( true ),
ÿÿÿÿÿÿÿ m_fn( std::move( fn ) ),
ÿÿÿÿÿÿÿ m_pDep( pDep )
ÿÿÿÿ{
ÿÿÿÿÿÿÿ assert(std::is_void_v<Dependent> || pDep);
ÿÿÿÿ}
ÿÿÿÿdefer( defer const & ) = delete;
ÿÿÿÿvoid operator =( defer const & ) = delete;
ÿÿÿÿ~defer()
ÿÿÿÿ{
ÿÿÿÿÿÿÿ run();
ÿÿÿÿ}
ÿÿÿÿvoid run() noexcept
ÿÿÿÿ{
ÿÿÿÿÿÿÿ constexpr bool DepRunnable = requires { { m_pDep->run() }; };
ÿÿÿÿÿÿÿ static_assert( DepVoid || DepRunnable );
ÿÿÿÿÿÿÿ if( !m_enabled )
ÿÿÿÿÿÿÿÿÿÿÿ return;
ÿÿÿÿÿÿÿ m_fn();
ÿÿÿÿÿÿÿ m_enabled = false;
ÿÿÿÿÿÿÿ if constexpr( DepRunnable )
ÿÿÿÿÿÿÿÿÿÿÿ m_pDep->run();
ÿÿÿÿ}
ÿÿÿÿvoid disable() noexcept
ÿÿÿÿ{
ÿÿÿÿÿÿÿ constexpr bool DepDisable = requires { { m_pDep->disable() }; };
ÿÿÿÿÿÿÿ static_assert( DepVoid || DepDisable );
ÿÿÿÿÿÿÿ m_enabled = false;
ÿÿÿÿÿÿÿ if constexpr( DepDisable )
ÿÿÿÿÿÿÿÿÿÿÿ m_pDep->disable();
ÿÿÿÿ}
private:
ÿÿÿÿbool m_enabled;
ÿÿÿÿNO_UNIQUE_ADDRESS Fn m_fn;
ÿÿÿÿstruct empty { empty( void * ) {} };
ÿÿÿÿstatic constexpr bool DepVoid = std::is_void_v<Dependent>;
ÿÿÿÿusing dep_t = std::conditional_t<std::is_void_v<Dependent>, empty, Dependent *>;
ÿÿÿÿNO_UNIQUE_ADDRESS dep_t m_pDep;
};
template<typename T>
struct scope_exit {
ÿÿÿÿT f;
ÿÿÿÿscope_exit(T f) : f(f) { }
ÿÿÿÿ~scope_exit() { f(); }
};
If you want to claim your solution is "better", start with an example or
two showing what you can do with your version, that cannot be done with standard or common versions.ÿ At the moment, all you have done is shown
how you can add an apparently pointless "Dependent" function that must
have a "run" method. ...
And if you are going to require the use of C++20 or concepts (not a bad idea), then at least use them consistently - put the concepts where they belong, restricting the types of the template, rather than in static assertions. And drop the C-style "assert" for something that is known
at compile time.
A little excess irregularity, I feel; why not provide a static instance
of "empty" which can be used to terminate the list of deps then provide
an explicit template specialisation for the sugar of using "void"?
Furthermore "assert" is a bit risky because it's /always/ invalid to
flow the control onward when the condition isn't met, not only in builds other than NDEBUG, yet C++ has exceptions to abort and avoid invalid continuation too.
Do you mean it to be dual pattern either:
a) pointer to instance of any type with the required interface which
re-implements the necessary member functions
b) all work provided exactly when the client code has only a function
I feel there could be some improvement also by making the basic features
of disablement and forwarding available to derived classes which merely
then need to override a virtual member function to complete their
typical implementation. Which brings me to ...
I'm a bit out of date, I suppose "final" prevents derivation, but why
prevent it? Some need to restrict all "new" instances to some special
array, perhaps? It would be a bit odd.
Am 23.11.2026 um 13:26 schrieb David Brown:
template<typename T>
struct scope_exit {
ÿÿÿÿÿT f;
ÿÿÿÿÿscope_exit(T f) : f(f) { }
ÿÿÿÿÿ~scope_exit() { f(); }
};
You can't cascade multiple exits with that. This means if you have
several manipulations on a data structure and you want to revert them
if an exception occurs the destructor revers the changes so far. But
if nothing happens in that sense you simply call .disable() on the
last node in the chain and all dependent nodes are also disabled.
Just as a commit in a databse.
That is not an example - it's a vague idea of an imagined use-case.
Try showing an example, using your scope_exit.
Compare it with alternatives. ...
Then we will be able to see if and why your code is better,
and also get an idea whether it would be better to have
it in addition to normal scope_exit features, rather than
instead of them.
Am 23.11.2026 um 15:19 schrieb David Brown:
That is not an example - it's a vague idea of an imagined use-case.
Ususally I don't cascade. But I had this usecase multiple times when
applying several changes on a data structure where each operation
could fail with an exception.
Tryÿ showing an example, using your scope_exit.
Can't you imagine the purpose from the code - poor !
Compare it withÿ alternatives. ...
experimental::scope_exit hasn't cascading and no explicit run().
Then we will be able to see if and why your code isÿ better,
and also get an idea whether it would be better to have it in addition
to normal scope_exit features, rather than
instead of them.
You really have a lack of imagination.
Or you don't understand the code.
Claiming things are "obvious" or that others "lack imagination" is
an excuse for being unable or unwilling to explain your own thoughts.
If you have trouble explaining things, then the suggestions below will
help.ÿ If you are simply unwilling to do so, then why bother posting
the code in the first place?ÿ It is unusable to others as it is, and contains nothing particularly innovative.
Figure out what you want to achieve.ÿ Figure out how the code should be used.ÿ Specify it, describe it, document it.ÿ Show examples.ÿ Compare it
to existing solutions.ÿ Show why your "defer" class with "Dependent" pointers is necessary, and better than a single "standard" scope_exit function that does more than one thing, or multiple "standard"
scope_exit calls.
Explain why single "Dependent" is enough, rather than accepting multiple "Dependents".ÿ Explain what you mean by "Dependent" in the first place.I see: you didn't understand the code.
Am 23.11.2026 um 13:26 schrieb David Brown:
template<typename T>
struct scope_exit {
ÿÿÿÿT f;
ÿÿÿÿscope_exit(T f) : f(f) { }
ÿÿÿÿ~scope_exit() { f(); }
};
You can't cascade multiple exits with that. This means if you have
several manipulations on a data structure and you want to revert them
if an exception occurs the destructor revers the changes so far. But
if nothing happens in that sense you simply call .disable() on the
last node in the chain and all dependent nodes are also disabled.
Just as a commit in a databse.
Bonita Montero <Bonita.Montero@gmail.com> writes:
Am 23.11.2026 um 13:26 schrieb David Brown:
template<typename T>
struct scope_exit {
ÿÿÿÿT f;
ÿÿÿÿscope_exit(T f) : f(f) { }
ÿÿÿÿ~scope_exit() { f(); }
};
You can't cascade multiple exits with that. This means if you have
several manipulations on a data structure and you want to revert them
if an exception occurs the destructor revers the changes so far. But
if nothing happens in that sense you simply call .disable() on the
last node in the chain and all dependent nodes are also disabled.
Just as a commit in a databse.
KISS. This is from a 1992-ish port of Unix to run as
an actor for the Chorus microkernel.
//
// Destructor: undo what fork has done upon failure.
//
tForkFailure::~tForkFailure()
Am 23.11.2026 um 16:45 schrieb David Brown:
Claiming things are "obvious" or that others "lack imagination" is
an excuse for being unable or unwilling to explain your own thoughts.
It's just one screenpage of code. And the code itself is simpler than
any real world application. So where's the problem ?
If you have trouble explaining things, then the suggestions below will
help.ÿ If you are simply unwilling to do so, then why bother posting
theÿ code in the first place?ÿ It is unusable to others as it is, and
contains nothing particularly innovative.
Then you still don't understand what I did.
Figure out what you want to achieve.ÿ Figure out how the code should
be used.ÿ Specify it, describe it, document it.ÿ Show examples.
Compare it to existing solutions.ÿ Show why your "defer" class with
"Dependent" pointers is necessary, and better than a single "standard"
scope_exit function that does more than one thing, or multiple
"standard" scope_exit calls.
The ide is somewhat unusual, but as the code is very short it's
not hard to understand.
Explain whyÿ single "Dependent" is enough, rather than acceptingI see: you didn't understand the code.
multiple "Dependents".ÿ Explain what you mean by "Dependent" in the
first place.
The dependents could be cascaded.
Since you apparently cannot or will not give any explanation or
usage of the code, there is nothing more to be said about it. ...
In the future, I'd recommend you send such pointless code straight
to /dev/null and don't bother posting anything you are unwilling
to talk about.
On Mon, 23 Nov 2026 16:37:32 GMT
scott@slp53.sl.home (Scott Lurndal) gabbled:
Bonita Montero <Bonita.Montero@gmail.com> writes:
Am 23.11.2026 um 13:26 schrieb David Brown:
template<typename T>
struct scope_exit {
ÿÿÿÿT f;
ÿÿÿÿscope_exit(T f) : f(f) { }
ÿÿÿÿ~scope_exit() { f(); }
};
You can't cascade multiple exits with that. This means if you have >>>several manipulations on a data structure and you want to revert them
if an exception occurs the destructor revers the changes so far. But
if nothing happens in that sense you simply call .disable() on the
last node in the chain and all dependent nodes are also disabled.
Just as a commit in a databse.
KISS. This is from a 1992-ish port of Unix to run as
an actor for the Chorus microkernel.
//
// Destructor: undo what fork has done upon failure.
//
tForkFailure::~tForkFailure()
Why is fork failure a specific class? Thats a bit wierd.
After this point, any resources allocated to the newly created
process must be deallocated if the fork() fails (e.g. due to
insufficient resources,
ÿÿÿÿÿÿÿ if( !m_enabled )
ÿÿÿÿÿÿÿÿÿÿÿ return;
ÿÿÿÿÿÿÿ m_fn();
ÿÿÿÿstatic_assert( std::is_nothrow_invocable_v<Fn> );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
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.
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.
Does C++ still allow noexcept(false) destructors and does an exceptionYes, you can do that; but I guess that's very uncommon.
at the end of a destructor allow the members to be destroyed? ...
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.
ÿÿ 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 exceptionYes, you can do that; but I guess that's very uncommon.
at the end of a destructor allow the members to be destroyed? ...
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.
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 exceptionYes, you can do that; but I guess that's very uncommon.
at the end of a destructor allow the members to be destroyed? ...
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.
Am 24.11.2026 um 13:42 schrieb Tristan Wibberley:
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).
It wasn't handled differently with any C++-version, meaning
that destructors that throw exceptions are nearly no existing.
So I think it's really common.
It's dangerous: If an exception occurs while unwinding the stack of a function the unwinding continues and when it is finished the exception
is propagated upwards. But if you have a second exception while this unwinding happens the application is terminated.
You can throw and handleHang on, my memory is kicking-in some nuance and that the above is not
exceptions within the destructor without terminating, it is only leaking
of the secondary exception out of the destructor that causes termination.
On 24/11/2026 14:01, Bonita Montero wrote:
Am 24.11.2026 um 13:42 schrieb Tristan Wibberley:
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).
It wasn't handled differently with any C++-version, meaning
that destructors that throw exceptions are nearly no existing.
So I think it's really common.
It's dangerous: If an exception occurs while unwinding the stack of a
function the unwinding continues and when it is finished the exception
is propagated upwards. But if you have a second exception while this
unwinding happens the application is terminated.
That's what I think is the goal in many applications. That's what noexcept(false) is for. noexcept(true) is for when you want the
termination every time an exception would leak from a destructor with
runtime checks in case you made a booboo.
When the invariants are not maintained it's dangerous to continue, the
future should be broadly as if power were lost by the host or a process
kill occurs (memory unit mapped into process memory is lost, etc).
If you could continue safely then you can re-establish the invariant
within the destructor by all sorts of means. You can throw and handle exceptions within the destructor without terminating, it is only leaking
of the secondary exception out of the destructor that causes termination.
The rest of the system should gracefully continue thereby making it safe
(eg, failover, rapid restart, etc).
Safety involves not running a program segment when its invariants do not hold. That much is fundamental. If you can run it then its invariants
perhaps are more conditional after all (perhaps the system has processes
and failover, stochastic redundancy and quora, etc) and maybe then you
don't need any cleanup for a code segment (no problem no scope guard).
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.
destructors should have no side effects (that can throw)
On 24/11/2026 19:22, Ross Finlayson wrote:
destructors should have no side effects (that can throw)
That's merely a deduction from contensive ontology which, as you know,
is typically a matter of fantasy: 'I call them "destructor" therefore
they should not throw'. But their extensional properties are of things
that can throw: "Throwing in them can mean the right thing therefore
they may throw".
In fact they're scope-hooks at the same time so which of the succeeding program addresses (Dijkstra) should run after they run and in which conditions is the important question: "all of them all of the time" is
not correct.
| Sysop: | Tetrazocine |
|---|---|
| Location: | Melbourne, VIC, Australia |
| Users: | 3 |
| Nodes: | 8 (0 / 8) |
| Uptime: | 51:06:31 |
| Calls: | 223 |
| Files: | 21,513 |
| Messages: | 83,422 |