Date: Wed, 21 May 2025 09:25:06 +0100
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);
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);
>
> 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);
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);
Received on 2025-05-21 08:25:19