C++ Logo

std-proposals

Advanced search

Re: std::optional - Inconsistency/defect regarding constexpr

From: Maciej Cencora <m.cencora_at_[hidden]>
Date: Mon, 28 Sep 2020 11:46:56 +0200
I guess so.

If you are planning to do it, could you please propose also updates to
std::variant?

Regards,
Maciej

sob., 26 wrz 2020 o 01:32 Michael Scire <sciresm_at_[hidden]> napisał(a):

> Okay, that makes a kind of sense.
>
> Would the thing to do be to submit a proposal to update the relevant
> optional functions, then?
>
>
> On Fri, Sep 25, 2020 at 4:51 AM Maciej Cencora <m.cencora_at_[hidden]>
> wrote:
>
>> ... was updated to allow changing active union member **in constexpr
>> context** (P1330).
>>
>> pt., 25 wrz 2020 o 13:49 Maciej Cencora <m.cencora_at_[hidden]> napisał(a):
>>
>>> It is not an oversight. It's just nobody has proposed updates in the
>>> standard library after C++20 language was updated to allow changing active
>>> union member (P1330).
>>>
>>> Regards,
>>> Maciej
>>>
>>>
>>> czw., 24 wrz 2020 o 10:13 Michael Scire via Std-Proposals <
>>> std-proposals_at_[hidden]> napisał(a):
>>>
>>>> The rules surrounding constexpr for std::optional seem inconsistent --
>>>> like there are a lot of functions that are "just" missing the keyword due
>>>> to oversight.
>>>>
>>>> For example, `optional(std::nullopt_t)` is constexpr, but `optional
>>>> &operator=(std::nullopt_t)` is not.
>>>>
>>>> This leads to the following (https://godbolt.org/z/8a9Ga3):
>>>>
>>>> // Okay
>>>> ```
>>>> constexpr std::optional<int> ValidOptional = []{
>>>> std::optional<int> opt(std::nullopt);
>>>> opt = std::optional<int>(std::nullopt);
>>>> return opt;
>>>> }();
>>>> ```
>>>>
>>>> // Compiler error
>>>> ```
>>>> constexpr std::optional<int> InvalidOptional = []{
>>>> std::optional<int> opt(std::nullopt);
>>>> opt = std::nullopt;
>>>> return opt;
>>>> }();
>>>> ```
>>>>
>>>> Other oversights:
>>>> `template<typename U> optional(U &&)` is constexpr, but
>>>> `template<typename U> optional &operator=(U &&)` is not.
>>>>
>>>> Things that are less obviously oversights, but might want to change:
>>>> `emplace` is not constexpr, even though the constructors it would call
>>>> are.
>>>> `reset` is not constexpr.
>>>> `swap(optional &)` is not constexpr, nor is std::swap for optionals.
>>>> (std::swap is constexpr for other types).
>>>> copy constructor/move constructor for template<typename T> optional<U>
>>>> are not constexpr, even though optional(U &&) is.
>>>> If the following changed, copy/move assignment operators would probably
>>>> also want to be constexpr.
>>>>
>>>> --
>>>> Std-Proposals mailing list
>>>> Std-Proposals_at_[hidden]
>>>> https://lists.isocpp.org/mailman/listinfo.cgi/std-proposals
>>>>
>>>

Received on 2020-09-28 04:47:11