4 ms·
If my pointer already supports NULL, why would I put std::optional on top of it? I was saying I don't need a null for anything but a pointer.
by mehrdadn 8y ago
If my pointer already supports NULL, why would I put std::optional on top of it? I was saying I don't need a null for anything but a pointer.
- cmrdporcupine 8y agoMainly to avoid scenarios where a pointer is used instead of a value type only because the author wishes to express that it is optional.
- gnulinux 8y agoBecause it's a 0 overhead abstraction and guard against the case when you (or a coworker) forget to check for null. Are we seriously gonna pretend like you never caused a null pointer exception?
- masklinn 8y agostd::optional doesn't do that though, you can deref an empty optional and the result's the same as deref'ing an empty unique_ptr or a null pointer: UB.
- basil-rash 8y agoBut it makes you stop and thing before doing so. Thats the whole point.
- masklinn 8y agoIt doesn't do that any more than a pointer
- mehrdadn 8y agoI don't see how it's zero overhead? And honestly while it's obviously not never, it's not exactly frequent either.
- gnulinux 8y agoIt's a zero overhead abstraction because it'll compile to the same code.