🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

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

Re: A better experimental::scope_exit

#1003
From: David Brown
Date: Mon, 23 Nov 2026 12:26
87 lines
4312 bytes
On 21/11/2026 18:22, Bonita Montero wrote:
> #pragma once
> #include <type_traits>
> #include <cassert>
> #include <utility>
> #include "nui.hpp"

A dependency on a non-standard header - if your code really is dependent 
on "nui.hpp" - makes your code useless to anyone else.

Apart from that, what makes you think your version is "better" than the 
the proposed standard version in "experimental::" ?

Or the C++ "defer" version here?

<https://gustedt.wordpress.com/2025/01/06/simple-defer-ready-to-use/>

Or any other simple RAII wrapper like :

template<typename T>
struct scope_exit {
	T f;
	scope_exit(T f) : f(f) { }
	~scope_exit() { f(); }
};

or a template on a lambda (like in Gustedt's version) ?

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 
for 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.



> 
> 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;
> };
Technical Details
Message-ID: <11e1bd7$1rqma$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-mail
References: <11dsk17$8ske$2@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: David Brown <david.brown@hesbynett.no>
Newsgroups: comp.lang.c++
Subject: Re: A better experimental::scope_exit
Date: Mon, 23 Nov 2026 13:26:14 +0100
Organization: A noiseless patient Spider
Lines: 87
Message-ID: <11e1bd7$1rqma$1@dont-email.me>
References: <11dsk17$8ske$2@raubtier-asyl.eternal-september.org>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
Injection-Date: Mon, 23 Nov 2026 12:26:16 +0000 (UTC)
Injection-Info: dont-email.me;
	logging-data="1960650"; mail-complaints-to="abuse@eternal-september.org";	posting-account="U2FsdGVkX1+2xdcFYT21AJKYv4Yjk1GOuLXOqZKztD0="; posting-host="4c393d4ea759cbe76e4062837f9f8a62"
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:kKTYGabWlDEo5BDt1PP8zDmctrU= sha256:R/bWlDvVWvLyiMM7xRCrcyGUn+BpgkKcFKWr5qTGJxo=
	sha1:nTEnkl3Arez0fhfvuVRJeQnZcO4= sha256:UEF8Z3RhTHiLRPAHQxsxc4BGFjF3/jqaVFm4tnn0PBw=
In-Reply-To: <11dsk17$8ske$2@raubtier-asyl.eternal-september.org>
Content-Language: en-GB, nb-NO