C++ Logo

std-proposals

Advanced search

Re: [std-proposals] finally keyword

From: Igor Ingultsov <ingultsov_at_[hidden]>
Date: Mon, 2 May 2022 10:47:37 +0200
Hi,

but why? You have RAII, use it. It's the proper way to deal with resource
cleanup.

struct _finally()
{
  _finally(std::function<void(void)> f) _f(f) : {}
  ~_finally() ( _f(); };
  std::function<void(void)> _f;
};

my_function() {
  // let's define our finally
  _finally f([](){
       cleanup();
  });

  try {
  } catch(...) {}
  // whatever.

} // here your _finally will be destroyed, which will call the d'tor
~_finally() and your lambda in d'tor will be called.
But this is a lazy bodge. You could nicely encapsulate your resource as a
class and get RAII out of the box.

BR,
Igor


Am So., 1. Mai 2022 um 09:52 Uhr schrieb Abdullah Qasim via Std-Proposals <
std-proposals_at_[hidden]>:

>
>
> try {
>
> something();
>
> } catch (...) {
>
> std::cerr << "exception caught!!";
>
> }
>
>
>
> std::clog << "No except caught!";
>
>
>
> finally { // already Microsoft extension, but
>
> cleanup(); // should be in core language
>
> }
>
>
>
>
> --
> Std-Proposals mailing list
> Std-Proposals_at_[hidden]
> https://lists.isocpp.org/mailman/listinfo.cgi/std-proposals
>

Received on 2022-05-02 08:47:50