C++ Logo

std-proposals

Advanced search

[std-proposals] treat alternative as subobject when pattern-matching variant-like types

From: Ðì Ò»·² <xuyifan2004_at_[hidden]>
Date: Sat, 10 Jan 2026 13:19:31 +0000
In P2688R5 pattern matching, variant-like and polymorphic types share the similar syntax to match alternatives or subtypes. This syntax is confusing for implying the type of alternatives to be a subtype. Also, this syntax makes it awkward to distinguish between the variant-like itself and its alternatives.

In P2688R5, 'auto: let' is introduced to distinguish from 'let':
auto: let alternative => //...
let self => //...

I suggest to treat alternative as subobject, which is far more straightforward:
[let alternative] => //...
let self => //...

It can also be applied to pointer-like types such as optional, free from reusing '?':
[let some] => //...
nullptr => //...
nullopt => //...

The reason for the new syntax is that an alternative or a pointed-to is a distinct object beneath the matched object, rather than just a different interpretation of the same object. The old syntax should be only applied to type matching which can be performed through built-in casts.

Received on 2026-01-10 13:19:35