On 07.08.2015 03:09, Marcel Ebmer wrote:
Hi Everyone,
What if you could do
std::mutex the_mutex; using Lock = std::unique_lockstd::mutex; void f() { with(Lock{the_mutex}) { do_something(); do_other_thing(); } }
*...or...*
struct Pushed_matrix { Pushed_matrix() { glPushMatrix(); } ~Pushed_matrix() { glPopMatrix(); } };
void f() { with(Pushed_matrix{}) draw_something(); }
At C++Now (2015), I presented this idea of mine and a macro implementation (BOOST_WITH) in a lightning talk. Since reception was good and more serious than the talk itself, I continued to work on it. I would appreciate informal review and opinions:
I don't think this utility gives much compared to the naive macro-less
solution:
f() {
{
Lock lock{the_mutex});
do_something();
do_other_thing();
}
do_something_without_a_lock();
}
Also, your macro requires the type to be movable. This seems like a
rather strong requirement. For instance, it won't work with lock_guard.
If you really want to improve it you might want to use 'for' statement
instead of 'if', something along these lines:
template< typename T >
struct with_value
{
T value;
bool guard;
};
#define BOOST_WITH(x)\
for (with_value