C++ Logo

std-proposals

Advanced search

Re: [std-proposals] Requires-clause for data members

From: Jonathan Wakely <cxx_at_[hidden]>
Date: Thu, 15 Feb 2024 11:14:37 +0000
On Thu, 15 Feb 2024 at 11:13, Jonathan Wakely <cxx_at_[hidden]> wrote:

>
>
> On Thu, 15 Feb 2024 at 11:12, Marcin Jaczewski <
> marcinjaczewski86_at_[hidden]> wrote:
>
>> czw., 15 lut 2024 o 11:23 Jonathan Wakely via Std-Proposals
>> <std-proposals_at_[hidden]> napisaƂ(a):
>> >
>> >
>> >
>> > On Thu, 15 Feb 2024 at 09:54, Tiago Freire via Std-Proposals <
>> std-proposals_at_[hidden]> wrote:
>> >>
>> >> And what exactly would this do?
>> >
>> >
>> > Exactly what the standard does for subrange::size_ in
>> [range.subrange.general]
>> >
>> >
>> > make-unsigned-like-t<iter_difference_t<I>> size_ = 0; // exposition
>> only; present only
>> > // if StoreSize
>> is true
>> >
>> >
>> >
>> >
>> >>
>> >> -----Original Message-----
>> >> Subject: [std-proposals] Requires-clause for data members
>> >>
>> >> Has there been any discussion of requires-clauses for data members?
>> >> Something like:
>> >>
>> >> > template<int N>
>> >> > struct numbers {
>> >> > int x requires (N >= 1) = 0; // leading
>> >> > int y requires (N >= 2) = 0; // trailing };
>> >>
>> >> It seems grammatically possible if you mandate parentheses, and it
>> would certainly help people reduce the use of partial specializations in
>> these places.
>> >
>> >
>> >
>> https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2013/n3613.pdf#section.5
>> has some discussion of an earlier idea like this, based on features in the
>> D language.
>> >
>>
>> Then what will be the behavior of:
>>
>> ```
>> size_ = 13;
>> ```
>>
>> If we consider this as "zombi" name, we could issue a warning like:
>> "variable is deleted".
>> But it will work fine in code like:
>>
>> ```
>> void setSize(int i) require (StoreSize == true)
>> {
>> size_ = i;
>> }
>> void setSize(int) require (StoreSize == false)
>> {
>> // nothing
>> }
>> ```
>>
>> This will still be "viral" but at least we could easily reason about
>> code as `size_` always means the same thing but
>> in some cases is not accessible.
>>
>
> Yes, as discussed in
> https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2013/n3613.pdf#section.5
> which is why I linked to it :-)
>

But it's much simpler to just do:

if constexpr (StoreSize)
  size_ = n;

in the middle of another function, rather than adding a pair of overloaded
setters.

Received on 2024-02-15 11:15:53