I know that's not directly what you're asking for, but I found the other day that "C++ Core Guidelines" have a helper library "GSL_util" that provide something similar to Go's defer.
The implementation is very simple, it's just using a class for its destructor:
Of course, in C++ the need for such constructs is reduced due to RAII and scoping running destructors automatically. This is useful when you need to interact with C libraries that you don’t want to wrap in C++ (and when I was doing something like this in the past, I rolled my own as well).
The implementation is very simple, it's just using a class for its destructor:
template <class F>
class final_act
{
public:
private: };Source: https://github.com/microsoft/GSL/blob/ebe7ebfd855a95eb937831...