3 ms·
> ...its purpose is not to have a way to express "missing" items in a type-safe manner... Except that's exactly why you would take an `optional<int>` as a para
by humanrebar 8y ago
> ...its purpose is not to have a way to express "missing" items in a type-safe manner...
Except that's exactly why you would take an `optional<int>` as a parameter. To avoid the ambiguity and possible bugs of taking an `int*`.
- tzahola 8y agoYou mean int*, right?
- humanrebar 8y agoYes. Fixed! Thanks.
- masklinn 8y ago> Except that's exactly why you would take an `optional<int>` as a parameter. Except for the type-safe part which is the important bit (hence the emphasis on it), and which std::optional does not provide. > To avoid the ambiguity and possible bugs of taking an `int*`. The only ambiguity and possible bug (singular) you're avoiding using std::optional is the question of ownership. Because as emphasised in the original comment std::optional is no more type-safe than a raw pointer.
- humanrebar 8y agoClarity about ownership is enough. But there's also ambiguity because pointers are sometimes used to pass around arrays, especially in the case of 'char*'. And a dev also has to go out of his way to create a dangling optional<int>. Even if we consider just ambiguous ownership, there are a whole host of behaviors that could result from that single design mistake, including undefined behavior, which could include trashing your production database, firing the missiles, or behaving as expected until an unrelated piece of code changes a year later.