Lets ask you about this simple scope guard:
template <class T>
struct finop_t {
T& t;
~finop_t() { t(); }
};
#define FINALLY__(l, cl) \
auto FIN ## l ## clo = cl; \
finop_t<decltype(FIN ## l ## clo)> FIN ## l ## fin { FIN ## l ## clo}
#define FINALLY_(l, cl) FINALLY__(l, cl)
#define FINALLY(...) FINALLY_(__LINE__, ([=](){__VA_ARGS__}))
int main() {
FINALLY( std::cout << "hello" << std::endl ; );
std::cout << "one" << std::endl;
FINALLY( std::cout << "world" << std::endl ; );
std::cout << "second" << std::endl;
return 0;
}
Is it safe to rely on destruction order here? i.e. is it safe assume that ~finop_t()
will be called before lambda destructor?
Destruction of local variables takes place in the inverse order of their construction.
And here's a more efficient way, which needs no references and uses copy-elision to construct the lambda in-place.
(note, you may want to consider [&] rather than [=], but that's for you to judge)