Skip to content

Instantly share code, notes, and snippets.

@erincandescent
Created September 6, 2012 23:17
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save erincandescent/3661195 to your computer and use it in GitHub Desktop.
Save erincandescent/3661195 to your computer and use it in GitHub Desktop.
C++ with
template<typename TObj, typename TFn, typename... TArgs>
typename std::result_of<TFn(TArgs...)>::type with(TObj&& obj_, TFn fn, TArgs... args)
{
TObj obj(std::move(obj_));
return fn(std::foward(args)...);
}
template<typename TMutex>
std::lock_guard<TMutex>&& locked(TMutex& mtx)
{
return std::move(std::lock_guard<TMutex>(mtx));
}
// use
with(locked(mutex), [&]{
// do locked stuff
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment