How can I return a value, similar to null, with std::optional
?
My function receives an int index as parameter to iterate through a list, if the index value is not valid, how this std::optional
can be used to return a value similar to null?
How can I use std::optional to return a NULL value?
Asked Answered
nullptr
is to pointers what std::nullopt
is to std::optional
.
© 2022 - 2024 — McMap. All rights reserved.
return {};
doesn't work for you? – Obsolescentoptional
. So I'm not quite sure what you're asking. – Tionstd::optional
doesn't quite fit your use case (which isn't described in detail), you might consider usingstd::variant
withstd::monostate
as one of the variant types. – Literal