▲ | fluoridation 5 days ago | |||||||
std::optional does have dereference checking, but it's a run-time check: std::optional<T>::value(). Of course, you'll get an exception if the optional is empty, because there's nothing else for the callee to do. | ||||||||
▲ | BeetleB 4 days ago | parent | next [-] | |||||||
> but it's a run-time check And that's the problem. In other languages that have a Maybe type, it's a compile time check. If your code is not handling the "empty" case, it will simply fail to compile. I honestly don't see any value in std::optional compared to the behavior pre-std::optional. What does it bring to the table for pointers, for example? | ||||||||
| ||||||||
▲ | afdbcreid 4 days ago | parent | prev [-] | |||||||
But the dereference operator invokes UB if there is no value. Which is a recurring theme in C++: the default behavior is unsafe (in order to be faster), and there is a method to do the safe thing. Which is exactly the opposite of what it should be. | ||||||||
|