/deferxx

A Go's defer-like syntax scope guard idiom in C++11

Primary LanguageC++

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