4 ms·
An optional containing a pointer seems pretty dubious. You would usually either just use a pointer and have null mean empty or use an optional containing the po
by bobbyi_settv 8y ago
An optional containing a pointer seems pretty dubious. You would usually either just use a pointer and have null mean empty or use an optional containing the pointed-to type.
An optional pointer is a weird tri state thing where you can have empty or a null pointer or a non-null pointer.
- Koshkin 8y agoSemantically though, not having a pointer value and having a value - even null - are two different things. For example, doing pointer arithmetic in the former case would be meaningless.
- netheril96 8y agoNull pointer arithmetic is undefined, so just as pointless as arithmetic on an empty optional pointer. Your example fails to prove that optional pointer has any valid use.
- deleted 8y ago[deleted]
- Koshkin 8y agoSure, my point might seem theoretical; to explain further, the difference is that when std::optional pointer is used correctly its behavior in the absence of a value is perfectly defined! Which is a good thing.
- klodolph 8y agoIt is generally dubious but you could easily end up with an optional containing a pointer if you're using templates. The type would predictably leak into parts of the code base calling the templated code. There are a bunch of other parts of the C++ code base that are the same way. The most notorious one I can think of is std::declval, which just makes no sense whatsoever unless it appears inside another template.