4 ms·
> 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 i
by masklinn 9y 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 9y 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.