I propose that we should be able to achieve this in C++26 with some
simple syntax as follows:

There is similar functionality already proposed by wg21.link/P0052.
https://en.cppreference.com/w/cpp/experimental/scope_exit
However, I don't see the above implemented in the libstdc++ for GCC 12.1.