Macro enabling Python style 'with' in C++
[ Foreword: Note that I'm not going to argue whether or not it is a good idea to invent a new macro-based syntax for this.  I'm only reviewing your code. &mdash; end foreword ]

I think you have been pretty careful and cannot spot any serious issues with your implementation; nice job.

The only thing that I think could be improved is the error reporting.  Your static_assertion fires much too late in the instantiation process such that the error message will end up buried under loads of template error messages.  You can re-structure the instantiation machinery to improve this.

First, provide two overloads of boost::with_detail::make_true:

template &lt;class T&gt;
std::true_type make_true(T&amp;&amp;, std::false_type) {
    static_assert(std::is_move_constructible&lt;T&gt;::value,
                  "BOOST_WITH requires the scoped object's type to be move"
                  " constructible");
    return std::true_type{};  // never executed
}

template &lt;class T&gt;
always_true&lt;T&gt; make_true(T&amp;&amp; what, std::true_type) {
    static_assert(std::is_move_constructible&lt;T&gt;::value, "this is a bug");
    return always_true&lt;T&gt;{std::forward&lt;T&gt;(what)};
}


The static_assertion in the second overload is of course only paranoia and could be omitted.

Second, select the appropriate overload via tag dispatch:

#define BOOST_WITH(RESOURCE)                                        \
  if (auto BOOST_WITH_always_true = boost::with_detail::make_true(  \
      RESOURCE,                                                     \
      std::is_move_constructible&lt;decltype(RESOURCE)&gt;{}))


With this change, if I try to abuse your macro with a wrong type, GCC gives me this pretty straight-forward error message:

In file included from main.cpp:1:0:
boost/with.hpp: In instantiation of ‘std::true_type boost::with_detail::make_true(T&amp;&amp;, std::false_type) [with T = std::mutex; std::true_type = std::integral_constant&lt;bool, true&gt;; std::false_type = std::integral_constant&lt;bool, false&gt;]’:
main.cpp:22:5:   required from here
boost/with.hpp:42:7: error: static assertion failed: BOOST_WITH requires the scoped object's type to be move constructible
       static_assert(std::is_move_constructible&lt;T&gt;::value,


Compare this with what we would have got before.

Note that we cannot simply put static_assert(false, …); inside the make_true body because it would fire unconditionally on every compilation.  It is necessary to make the asserted condition dependent on the template argument.  Of course, any other template instantiated with T that always yields false would do equally well but using std::is_move_constructible (again) seems easiest.