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>


boost::optional is available for cases where the programmer wishes to pass by value, but still support a null value.
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 <boost/optional.hpp>
#include <iostream>
#include <iostream>
#include <optional>


boost::optional<int> maybeInt()
std::optional<int> maybeInt()


int main()
int main()
{
{
boost::optional<int> maybe = maybeInt();
std::optional<int> maybe = maybeInt();


if(!maybe)
if(!maybe)