C++ Logo

std-proposals

Advanced search

Re: [std-proposals] std::contains_mutable

From: Jason McKesson <jmckesson_at_[hidden]>
Date: Sat, 1 Jul 2023 14:52:26 -0400
On Sat, Jul 1, 2023 at 2:43 PM Frederick Virchanza Gotham via
Std-Proposals <std-proposals_at_[hidden]> wrote:
>
> On Saturday, July 1, 2023, Sebastian Wittmeier via Std-Proposals <std-proposals_at_[hidden]> wrote:
>>
>> Changing a mutable counts as read-only access from the view from outside the class.
>>
>> You think of non-mutable const accesses to be implicitly thread-safe?
>>
>> You could possibly reintroduce volatile member functions as thread-safe interface?
>>
>> C++: Been there, tried that?
>
> I don't understand what you wrote there.

I believe his point is that mutability is not relevant; thread-safety is.

If a member function is `const`, it could still technically modify
`mutable` members. But that doesn't matter; by marking it `const`, you
are de-facto claiming that calling this function on the same object
from multiple threads is 100% OK. And if you're doing modifiable
accesses, it is that function's job to *make it* OK.

So it shouldn't matter if a type has "mutable members"; what matters
is whether a particular interface is declared `const` or not. If it
is, then it's thread-safe.

> But anyway, let's say I have two base classes:
>
> Reservable_CanBeReadOnly
> Reservable_CannotBeReadOnly
>
> The former has two methods, 'Reserve' and 'ReserveReadOnly', while the latter only has 'Reserve'.
>
> So next I would have:
>
> template<class T>
> class Reservable : public std::conditional_t< std::contains_mutable_v<T>, Reservable_CannotBeReadOnly, Reservable_CanBeReadOnly >
> {
>
> };
>
> So if T is a class that has mutable members, then you won't be able to call the 'ReserveReadOnly' method.

I don't understand the relationship between your "Reservable" and `T`.
That is, what does it matter to `Reservable` if `T` has mutable
members? What code inside the class becomes invalid?

Received on 2023-07-01 18:52:36