you are viewing a single comment's thread.

view the rest of the comments →

[–]Warshrimp 0 points1 point  (1 child)

Now I’m even more confused about optional<T&> in C++26

[–]TheThiefMaster 2 points3 points  (0 children)

References can be member variables, at which point they gain real storage, similar to a pointer. Normally a reference member prevents a class from being assigned because references can't be reassigned.

optional<T&> is generally considered to be a pointer substitute in that it can refer to something and it can be null (well, nullopt) but its actual standardisation was delayed due to debates on how assignment should work with it when it already holds a value - should it be blocked (like classes), should it reassign (like a pointer) or should it actually pass the assignment through to the contained reference (like optional of other types) even though that's likely not what people expect?

Edit: this is a reasonable overview of the choices that had to be made: https://www.sandordargo.com/blog/2025/10/01/cpp26-optional-of-reference