Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RAII function invoke

Tags:

c++

lambda

Is there a class in standard library that will invoke provided function in its destructor? Something like this

class Foo
{
public:
 template<typename T>
 Foo(T callback)
 {
   _callback = callback;
 }
 ~Foo()
 {
   _callback();
 }
private:
 std::function<void()> _callback;
};
auto rai = Foo([](){ cout << "dtor";});
like image 719
konradk Avatar asked Nov 24 '25 17:11

konradk


1 Answers

there is an experimental scope_exit


example: https://godbolt.org/z/4r54GYo33

like image 183
apple apple Avatar answered Nov 26 '25 10:11

apple apple



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!