Using a sentinel value usually winds up being a bad idea. A negative value might not be valid today, but functionality tends to change over time. Someday it might become a perfectly valid value and, all of a sudden, some things don't seem to work anymore. Also, programmers tend to be lazy, if a function returns a value, they will tend to just use the value and not bother to check if it's in a valid range. At least by using
std::optional
or an exception, they are forced to acknowledge that there could have been an error. They may still ignore it, but they are aware of it. —
Ferruccio 48 secs ago