Skip to content

Instantly share code, notes, and snippets.

@allenh1
Created October 1, 2017 04:47
Show Gist options
  • Select an option

  • Save allenh1/daa97909aea302d65ff0b59ba607f898 to your computer and use it in GitHub Desktop.

Select an option

Save allenh1/daa97909aea302d65ff0b59ba607f898 to your computer and use it in GitHub Desktop.
This is a dank file
#include <functional>
#include <iostream>
#include <memory>
template<typename Callable>
struct ScopeExit
{
explicit ScopeExit(Callable _callable)
: m_callable(_callable) {}
~ScopeExit() { m_callable(); }
private:
Callable m_callable;
};
template<typename Callable>
ScopeExit<Callable>
make_scope_exit(Callable callable)
{
return ScopeExit<Callable>(callable);
}
struct memes {
explicit memes(int * _memes) {
m_memes = _memes;
}
void operator () () {
std::cout<<"int is: "<<*m_memes<<std::endl;
}
private:
int * m_memes = nullptr;
};
struct A {
~A() { std::cout<<"call to destructor"<<std::endl; }
};
int main()
{
int * memes = new int();
*memes = 70;
auto my_a = std::make_shared<A>();
auto at_exit = make_scope_exit([&memes]() {
std::cout<<"int is: "<<*memes<<std::endl;
delete memes;
});
std::cout<<"This prints first!"<<std::endl;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment