C++ Logo

std-proposals

Advanced search

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

From: Marcin Jaczewski <marcinjaczewski86_at_[hidden]>
Date: Thu, 15 Feb 2024 12:27:50 +0100
czw., 15 lut 2024 o 12:18 Jonathan Wakely <cxx_at_[hidden]> napisał(a):
>
>
>
> On Thu, 15 Feb 2024 at 11:14, Jonathan Wakely <cxx_at_[hidden]> wrote:
>>
>>
>>
>> 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.
>
>
> And I don't think a warning is appropriate. Using a non-existent member should be ill-formed, just as it is today if you implement a "not-always-present" member via indirection through a subobject of class template type where some specializations don't have the member.
>

Yes, it should be an error but I think it should be a specific message
like "member is removed by require clause" instead of "unknown member
name".

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