I know that one can implement functions with optional parameters like so:
int someFunction(int A, int B = -1) {
if (B != -1) {
... // If B given then do something
} else {
... // If B not given then do something else
}
}
However, I'd lake to take advantage of std::optional as recommended by a coworker. Here's what I'm trying to do but I'm getting errors:
int some Function(int A, std::optional<int> B) {
if (B.has_value()) {
... // If B given then do something
} else {
... // If B not given then do something else
}
}
The problem is that in the first approach, I can call the function like so someFunction(5) and C++ will realize I've opted not to use the optional parameter. But in the second aproach calling someFunction(5) in the same way will produce the error too few arguments to function call.
I want to be able to call the function without including the optional argument with the second approach, is this possible/recommended?