templates are weird! • omg boost takes so long to compile • i’ll have to manually manage every memory allocation • gahhh derp derp herrrrrrrrp // hacker news
• Crappy acronym, simple concept • Acquire resources in constructors, release them in destructors • Deterministic destruction does the rest • Java try-with-resources is an ugly hack
do_something()! {! // acquires lock in my_object! myobj_.do_something()! }! ! // destroyed automaticaly when object goes out of scope! shared_ptr<my_object> myobj_;! };! ! int main(void)! {! auto o = std::make_shared<my_other_obj>();! o->do_something();! return 0;! }!
"hello, world\n"; };! // prints "hello, world” ! my_simple_lambda();! int i = 0;! // captures i by copy! // prints 2, but i is unchanged in outer scope;! auto my_copy_capture_lambda = [i]() { i+=1; cout << i; };! my_copy_capture_lambda();! ! // captures i by copy! // prints i+y, i in outer scope is incremented by y! auto my_ref_capture_lambda = [&i](int y) { i+=y; cout << i; };! my_ref_capture_lambda();! ! // [&] = capture all by reference;! // [=] = capture all by copy! }!