2 ms·
In case someone wants the same in C++, the Guidelines Support Library comes with the class "final_action" and the function "finally()". Check the implementatio
by dgellow 6y ago
In case someone wants the same in C++, the Guidelines Support Library comes with the class "final_action" and the function "finally()".
Check the implementation here: https://github.com/microsoft/GSL/blob/master/include/gsl/gsl_util#L76 https://github.com/microsoft/GSL/blob/master/include/gsl/gsl....
Example from https://docs.microsoft.com/en-us/cpp/code-quality/c26448?view=vs-2019 https://docs.microsoft.com/en-us/cpp/code-quality/c26448?vie...:
void poll(connection_info info)
{
connection c = {};
if (!c.open(info))
return;
auto end = gsl::finally([&c] { c.close(); });
while (c.wait())
{
connection::header h{};
connection::signature s{};
if (!c.read_header(h))
return;
if (!c.read_signature(s))
return;
// ...
}
}
I love this pattern, it's a very nice way to have a kind of RAII but with more control and flexibility.