4 ms·
No, this isn't java/c#. In C++ only a pointer can be set to nullptr or NULL. std::optional is a value that can store a type T(T is a place holder for a type n
by beached_whale 8y ago
No, this isn't java/c#. In C++ only a pointer can be set to nullptr or NULL. std::optional is a value that can store a type T(T is a place holder for a type name). If T is a pointer (such as T * or int *...) then yes you could return null. But that is rare and looks weird.
So the value of an optional is either a valid value or it has no value. The default constructed std::optional<T> has no value
- pjmlp 8y agoC# has value types, you can't set structs to null.
- beached_whale 8y agoTrue. Most things are reference types though
- pjmlp 8y agoNo in the case of Nullable, which behaves just like std::optional.
- beached_whale 8y agoI see what you are saying, yes. Also, in c++ if you try to use an empty value in an optional<T> it isn't using undefined behavior as dereferencing a null pointer would, plus with optional you get value semantics so things like copy are deep vs copying the pointer. Not sure about C# nullables, but I guess if you copy the .value member it is deep.