Boost logo

Boost :

Subject: Re: [boost] [Utility] intuitive scoping with BOOST_WITH
From: Anton Bikineev (ant.bikineev_at_[hidden])
Date: 2015-08-08 15:44:19


Andrey Semashev <andrey.semashev <at> gmail.com> writes:

> 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<decltype(x)> w{x, true}; w.guard; w.guard=false)

Hi Andrey,

Sorry, didn't get the idea. How does for loop solve the problem with
non-movable types? I'd also note that it's possible to do that as follows

  #define WITH_IMP(code) code }
  #define WITH(x) {const decltype(x)& _unique = x; (void)_unique; WITH_IMP

  std::mutex m;
  WITH(std::lock_guard<std::mutex>{})
  (
    do_something();
  )

But in this case habitual brace syntax is lost. So after all I would rather stick
to usual C++ scoped style.


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk