From: Andrey Semashev (andrey.semashev_at_[hidden])
Date: 2023-03-22 10:17:47


On 3/22/23 12:38, Dominique Devienne wrote:
> On Wed, Mar 22, 2023 at 10:04 AM Andrey Semashev via Boost
> <boost_at_[hidden] <mailto:boost_at_[hidden]>> wrote:
>
>   error_code ec;
>   scope_fail guard(
>     [&] { std::cout << "Failure" << std::endl; },
>     [&ec] { return !!ec; });
>
>
> Having made the dumb mistake of writing once or twice `scope_fail(...);`
> instead of `scope_fail guard(...);`, is there a way to prevent that?

One way would be to use BOOST_SCOPE_FINAL. :)

> Like [[nodiscard]] with an alternate form like `auto guard =
> scope_fail(...);` ?
> I guess that requires move'ability, which might not be desirable?

`auto guard = scope_fail(...);` would work, as scope_fail is moveable.
With C++17, I think, you are even guaranteed to elide the move constructor.

[[nodiscard]] is not usable here as there's nothing to apply it to, as
we're talking about the scope guard constructor.