C++ Logo

std-proposals

Advanced search

Re: [std-proposals] New function attribute [[nodiscard_scope]]

From: Frederick Virchanza Gotham <cauldwell.thomas_at_[hidden]>
Date: Sat, 1 Jul 2023 00:08:08 +0100
On Fri, Jun 30, 2023 at 2:37 PM Jason McKesson via Std-Proposals
<std-proposals_at_[hidden]> wrote:
>
> I do not believe there is a coherent set of rules that would make the
> first two viable but not the third. You can vaguely gesture towards
> "scope" or whatever, but we're talking about a change to the standard.
> It has to be precise and specific.


Actually another idea came to me just now.

What if the "std::synchronized_value" template class were to be
changed so that, instead of invoking the method 'synchronize', you had
to use a helper class as follows:

    std::synchronized_value<T> g_obj;

    void Func(void)
    {
        Synced mylock(g_obj);

        mylock->SomeMethod();
    }

Then what we could do is place a restriction on the 'Synced' class so
that you can never have a temporary of it, something like:

    template <class T>
    class Synced {
        [[no_temp]] Synced( std::synchronized_value<T> &arg )
        {
            // Lock the lock
        }
    };

This would produce a compiler warning/error if you were to do the following:

    auto &r = *Synced(g_ojb); // ERROR - cannot create temporary of
type 'Synced'
    r.SomeMethod();

Received on 2023-06-30 23:08:21