October 4th, 2021

Some lesser-known powers of std::optional

C++17 introduced std::optional<T> which lets you augment the values of a type T with a bonus value known as std::nullopt which semantically represents the absence of a value. A std::optional which holds the value std::nullopt is known as empty.

The basic operations on std::optional are

  • Checking if it holds a value (has_value())
  • Retrieving the value (value())
  • Assigning a value (=)
  • Clearing the value and returning to the empty state (reset())

There are other lesser-known powers of the std::optional.

Contextual conversion

If used in places where the language expects a Boolean (as the controlling expression for if, while, for, ?:, or on either side of a || or &&), a std::optional is truthy if is has a value and falsy if it is empty.

if (opt)

is the same as

if (opt.has_value())

Note that this does not test whether the wrapped value is falsy.

std::optional<bool> opt1 = false;
if (opt1) {
    // this executes because the variable
    // is non-empty (even though it is false)
}

std::optional<void*> opt2 = nullptr;
if (opt2) {
    // this executes because the variable
    // is non-empty (even though it is nullptr)
}

My opinion: If T is itself contextually convertible to bool, write out opt.has_value() explicitly to avoid confusion.

Equality comparison against a value

An empty std::optional<T> compares unequal to any T.

std::optional<int> opt;
if (opt == 0) {
    // does not execute because the variable is empty
    // and is not equal to any integer.
}

My opinion: Use this instead of the more verbose if (opt.has_value() && opt.value() == 0).

Ordering comparison against a value

An empty std::optional compares less than any non-empty std::optional, and also less than any value.

std::optional<int> opt;
if (opt > 0) {
    // does not execute because "empty" is
    // less than all values
}

My opinion: Avoid except when sorting, because this behavior differs from NaN (another popular “There’s nothing useful here” value) in that the corresponding opposite-sense test does execute.

if (opt <= 0) {
    // executes because "empty" is less than all values
}

Instead, write it out as

    if (opt.has_value() && *opt > 0)
    // or
    if (opt.has_value() && *opt < 0)

Note that opt.value() and *opt both return the wrapped value but have different failure modes. The explicit opt.value() call will throw a std::bad_optional_access exception if the object is empty, whereas the *opt bypasses the verification and you get undefined behavior if the object turns out to be empty after all. In the above case, you can write the code equivalent as

    if (opt.has_value() && opt.value() > 0)
    // or
    if (opt.has_value() && opt.value() < 0)

because the compiler can optimize out the redundant emptiness test.

Topics
Code

Author

Raymond has been involved in the evolution of Windows for more than 30 years. In 2003, he began a Web site known as The Old New Thing which has grown in popularity far beyond his wildest imagination, a development which still gives him the heebie-jeebies. The Web site spawned a book, coincidentally also titled The Old New Thing (Addison Wesley 2007). He occasionally appears on the Windows Dev Docs Twitter account to tell stories which convey no useful information.

6 comments

Discussion is closed. Login to edit/delete existing comments.

  • Yuri Khan

    The eager defaulting method value_or is occasionally useful, too. (I call it “eager” because the default value argument is evaluated before the optional is checked for emptiness.)

    What std::optional<T> lacks is a monadic binding operator>>= and a lazy defaulting method or_else which would only evaluate the default value after making sure the optional is empty.

    • Daniel Sturm

      Yeah the standard library is missing a whole lot of things to make that feature shine and really useful. It almost seems like someone saw that options from various functional languages were getting really popular (even Java had it for years!) and decided they'd be a good idea in C++ as well (because clearly C++ must have every single feature anybody ever asked for) but didn't really understand the concept.

      You very rarely have to explicitly...

      Read more
      • word merchant · Edited

        Making sense of the latest C++ language perversions is not good for the mental health. If you wanted to make a programming language based on the insane gibbering and whispering of the mad elder gods who roamed the earth before man, it’d look a lot like C++, but probably a bit simpler. Even the gods of the Necronomicon would deem templates a bit risky.

    • Kinan Mahdi · Edited

      Yeah I was also kind of suprised value_or is not mentioned, as it actually solves some of the problems with the implicit conversion to bool

      std::optional opt1 = ?;
      if (opt1.value_or(false)) {
      //only executes when the optional holds the value true
      }
      std::optional opt2;
      if (opt2.value_or(nullptr)) {
      //only executes when the optional holds a non nullptr value
      }
      • Raymond ChenMicrosoft employee Author

        value_or() isn’t hidden, though. You hit the “.” and it shows up. But conversion operators and custom comparisons are hidden.

  • Neil Rashbrook

    The HTML encoding gods have not been kind today.