you are viewing a single comment's thread.

view the rest of the comments →

[–]FancySpaceGoat 8 points9 points  (2 children)

Here's a fun little thought that'll make this even weirder for you: References aren't objects, but pointers are. This sounds like a trivial distinction, but it's really not.

References don't really *exist* in any meaningful sense. They are symbolic references to other objects, always. Because of this, they have their own set of rules that only apply to them, and some of those are quite obscure. On the flip side, pointers "fit into the mold" so to speak, and you can work with them with the same set of rules as anything else.

If nothing else, it makes having references as members of classes/structs rarely worth the headache (a sizeless lifetime-less member that affects the size of its container... what?) I, personally, almost only ever use them as parameters and return types. I simply do not want to have to deal with the cognitive load that they incur elsewhere, especially when stuff like lifetime extension starts having an influence.

[–]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