A Go's defer-like syntax scope guard idiom in C++11
Switch branches/tags
Nothing to show
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
Makefile
README.rst
defer.h
example.cc

README.rst

Usage

defer ( <exps | statements> );
defer ( <exps | statements> ) namely ( <name> );

Example

if ((fd = mkstemp(buf)) < 0)
      return -1;
defer(close(fd); unlink(buf));  // auto-close, auto-delete when scope exits.

defer(vec_.pop_back()) namely(undoer);
vec_.push_back(x);
/* something may fail */
undoer.dismiss();               // strong guarantee

See also

[1]C++11(及现代C++风格)和快速迭代式开发 http://mindhacks.cn/2012/08/27/modern-cpp-practices/
[2]Generic: Change the Way You Write Exception-Safe Code ― Forever http://www.drdobbs.com/cpp/generic-change-the-way-you-write-excepti/184403758