On Sun, Jun 11, 2023 at 12:02 PM Jason McKesson wrote:
On Sun, Jun 11, 2023 at 11:57 AM Frederick Virchanza Gotham wrote:
> On Sat, Jun 10, 2023 at 3:39 PM Arthur O'Dwyer wrote:
> > See "The Auto() macro" for a way to do this without dummy names and with less visual noise.
> > https://quuxplusone.github.io/blog/2018/08/11/the-auto-macro/
>
> Here's how I do it:
[...]
> Note that I have a boolean to make it inactive, for example:
>
> {
>     OnScopeExit scoper( [](){ ::close(fd); } );
>
>     // Do something that might throw
>
>     // Do something else that might throw
>
>     // Do a third thing that might throw
>
>     // And then when we're happy it has succeeded:
>     scoper.active = false;    // Leave the file open
>     return;
> }

I fail to understand the point of your proposal then. If a specific
object needs to be talked to, then it needs to have a unique name.

+1.

For the record, the "Auto() macro" way of doing things would be that if you want the action to be conditional on a boolean variable under your control, you should just write that boolean variable explicitly in the code. That way (1) you "don't pay for what you don't use" when the boolean variable is unnecessary; (2) it's clear at the point of the "Auto" exactly what's going to happen later [the reader doesn't have to guess whether there's conditional logic hidden elsewhere]; and (3) contrariwise, there is no artificial limit to how complicated you can make the conditional logic — all you have to do is write it out. The "Auto() macro" way looks like this:

    {
        bool active = true;
        Auto(
            if (active) ::close(fd);
        );
        // Do something that might throw
        // Do something else that might throw
        // Do a third thing that might throw
        active = false; // Leave the file open
        return;
    }

–Arthur