C++ Logo

std-proposals

Advanced search

Re: [std-proposals] Execute statement when leaving scope

From: Sebastian Wittmeier <wittmeier_at_[hidden]>
Date: Fri, 23 Dec 2022 14:00:24 +0100
Apart from syntax: How could a compiler manage the code snippets to be called when leaving the scope normally or by (different?) exceptions?   You intended to make the user specify the scope (suggested by brackets); can those 'finalizers' be registered in an if block or for loop? The implementation soon gets non-trivial and people would like to customize how it is done. So the better approach could be to look at the existing facilities, whether they fulfill every important use case. And then, whether the syntax really needs to be improved by a dedicated core language feature.   Calling a code snippet at function exit is kind of an anti-pattern vs. RAII.   It probably is useful in some cases nevertheless. But then those examples could be analyzed, whether e.g. anonymous classes can be introduced in a simple way, which provide this functionality. (Or a single existing class template be instantiated.)   -----Ursprüngliche Nachricht----- Von:Frederick Virchanza Gotham via Std-Proposals <std-proposals_at_[hidden]> Gesendet:Fr 23.12.2022 10:55 Betreff:Re: [std-proposals] Execute statement when leaving scope An:std-proposals <std-proposals_at_[hidden]>; CC:Frederick Virchanza Gotham <cauldwell.thomas_at_[hidden]>; On Monday, December 12, 2022, Frederick Virchanza Gotham wrote: void Func(void) {     Handle h = GetResource("monkey");     if ( ! h ) return;     ~ { FreeDevice(h); }      // The rest of the function goes here }   Does anyone like my minimalistic simplistic syntax here? You don't need to include a header file nor write lambda syntax. Perhaps we could also make use of the 'throw' keyword to indicate how the scope was exited:  void Func(void) {     Handle h = GetResource("monkey");      if ( ! h ) return;      ~ { if throw FreeDevice(h); }       // The rest of the function goes here }  In the above code snippet, FreeDevice only gets called if Func is exited by means of a thrown exception. The two keywords 'if throw' side by side would have a special meaning here.     -- Std-Proposals mailing list Std-Proposals_at_[hidden] https://lists.isocpp.org/mailman/listinfo.cgi/std-proposals

Received on 2022-12-23 13:00:26