C++ Logo

std-proposals

Advanced search

Re: [std-proposals] std::contains_mutable

From: Frederick Virchanza Gotham <cauldwell.thomas_at_[hidden]>
Date: Sun, 2 Jul 2023 11:07:07 +0100
On Sun, Jul 2, 2023 at 1:26 AM Breno GuimarĂ£es wrote:
>
> I think the point folks are trying to make here is: Mutability doesn't imply anything for thread-safety.


Lots of compiler warnings/errors are for catching common programming
errors. Sometimes we get false positives, and in some cases we've even
devised ways of abating these false positives, for example by casting
to (void), or by putting double parentheses around an assignment in a
conditional.

Classes such as 'boost::synchronized_value' and also my own
'Reservable' are generic templates intended to be used for a wide
variety of purposes for all kinds of classes. It would make sense to
build in some compiler warnings/errors, even if they produce false
positives occasionally, such as:

    template <class T>
    requires ( false == std::contains_nonatomic_mutable<T> )
        class Reservable_CanReadOnly {
    };

The compiler error produced by this would be very helpful if T
contains a mutable non-atomic member. If the programmer is absolutely
certain that they know what they're doing, then they can use
'Reservable_CanReadOnly_Override' instead of 'Reservable_CanReadOnly',
defined as follows:

    template <class T>
    class Reservable_CanReadOnly_Base {
        /* implementation goes here */
    };

    template <class T>
    requires ( false == std::contains_nonatomic_mutable<T> )
    class Reservable_CanReadOnly : public Reservable_CanReadOnly_Base<T> {
    };

    template <class T>
    using Reservable_CanReadOnly_Override = Reservable_CanReadOnly_Base<T>;

We could make templates more safe, and avoid the introduction of
subtle bugs, by providing in <type_traits> the following two:

    std::contains_mutable
    std::contains_nonatomic_mutable

Received on 2023-07-02 10:07:15