Date: Wed, 21 May 2025 11:35:43 +0100
On Wed, 21 May 2025 at 09:25, Frederick Virchanza Gotham via
Std-Proposals <std-proposals_at_[hidden]> wrote:
>
> On Wed, May 21, 2025 at 9:12 AM Simon W wrote:
> >
> > The comma expression requires that the expressions on both sides of the colon are of the same type. But I think this syntax should be accepted:
> >
> > bool AllInUniversial();
> >
> > std::optional<int> r = AllInUniversial() ? 42 : std::nullopt;
>
>
> Looking at that ternary operator, it has three operands.
>
> Operand No. 1 is a PRvalue of type bool.
> Operand No. 2 is a PRvalue of type int.
> Operand No. 3 is an Lvalue of type std::nullopt_t
>
> So when the compiler encounters the following line of code:
>
> std::optional<int> r = AllInUniversial() ? 42 : std::nullopt;
>
> The quickest solution here is:
>
> std::optional<int> r = AllInUniversal() ? std::optional<int>(42) :
> std::optional<int>(std::nullopt);
That's unnecessarily verbose:
auto r = cond ? std::optional<int>(42) : std::nullopt;
You can even omit the <int> if you prefer to use CTAD.
>
> But were you hoping that it would be turned into something like the
> following under the hood?
>
> typedef std::optional<int> T;
> alignas(T) char unsigned storage[sizeof(T)];
> T &r = *static_cast<T*>(static_cast<void*>(&storage));
> if ( AllInUniversal() ) std::construct_at(&r, 42 );
> /****************/ else std::construct_at(&r, std::nullopt);
wat
Why would you want it to be turned into that, rather than what you wrote above?
std::optional<int> r = cond ? std::optional<int>(42) :
std::optional<int>(std::nullopt);
This already avoids any copy due to guaranteed elision. Your version
using a byte array and construct_at is just unnecessarily complex.
Std-Proposals <std-proposals_at_[hidden]> wrote:
>
> On Wed, May 21, 2025 at 9:12 AM Simon W wrote:
> >
> > The comma expression requires that the expressions on both sides of the colon are of the same type. But I think this syntax should be accepted:
> >
> > bool AllInUniversial();
> >
> > std::optional<int> r = AllInUniversial() ? 42 : std::nullopt;
>
>
> Looking at that ternary operator, it has three operands.
>
> Operand No. 1 is a PRvalue of type bool.
> Operand No. 2 is a PRvalue of type int.
> Operand No. 3 is an Lvalue of type std::nullopt_t
>
> So when the compiler encounters the following line of code:
>
> std::optional<int> r = AllInUniversial() ? 42 : std::nullopt;
>
> The quickest solution here is:
>
> std::optional<int> r = AllInUniversal() ? std::optional<int>(42) :
> std::optional<int>(std::nullopt);
That's unnecessarily verbose:
auto r = cond ? std::optional<int>(42) : std::nullopt;
You can even omit the <int> if you prefer to use CTAD.
>
> But were you hoping that it would be turned into something like the
> following under the hood?
>
> typedef std::optional<int> T;
> alignas(T) char unsigned storage[sizeof(T)];
> T &r = *static_cast<T*>(static_cast<void*>(&storage));
> if ( AllInUniversal() ) std::construct_at(&r, 42 );
> /****************/ else std::construct_at(&r, std::nullopt);
wat
Why would you want it to be turned into that, rather than what you wrote above?
std::optional<int> r = cond ? std::optional<int>(42) :
std::optional<int>(std::nullopt);
This already avoids any copy due to guaranteed elision. Your version
using a byte array and construct_at is just unnecessarily complex.
Received on 2025-05-21 10:36:00