Null object: Difference between revisions
Content added Content deleted
(→{{header|C++}}: Prefer C++17's std::optional over boost::optional since most modern compilers fully support C++17, but keep boost::optional as a blurb) |
|||
Line 389: | Line 389: | ||
}</lang> |
}</lang> |
||
std::optional is available since C++17 (or Boost's boost::optional via boost/optional.hpp for earlier standards) for cases where the programmer wishes to pass by value, but still support a null value. |
|||
<lang cpp> |
<lang cpp> |
||
⚫ | |||
#include <iostream> |
#include <iostream> |
||
⚫ | |||
std::optional<int> maybeInt() |
|||
int main() |
int main() |
||
{ |
{ |
||
std::optional<int> maybe = maybeInt(); |
|||
if(!maybe) |
if(!maybe) |