Checking if a std::optional has a value

What are the different ways to check if a std::optional contains a value?

There are several ways to check if a std::optional contains a value:

has_value()

This is an explicit way to check. It returns true if the optional contains a value, and false if it is empty.

#include <iostream>
#include <optional>

int main() {
  std::optional<int> maybe_int = 10;
  if (maybe_int.has_value()) {
    std::cout << "has value" << std::endl;
  } else {
    std::cout << "no value" << std::endl;
  }
}
has value

operator bool()

std::optional has a conversion operator to bool, so it can be used directly in boolean contexts like if statements. It returns true if the optional contains a value, and false if it is empty.

#include <optional>
#include <iostream>

std::optional<int> maybe_int = 10;
if (maybe_int) {
    std::cout << "has value" << std::endl;
} else {
    std::cout << "no value" << std::endl;
}
has value

Comparing to std::nullopt

You can directly compare an optional to std::nullopt to check if it is empty.

#include <iostream>
#include <optional>

int main() {
  std::optional<int> maybe_int = 10;
  if (maybe_int != std::nullopt) {
    std::cout << "has value" << std::endl;
  } else {
    std::cout << "no value" << std::endl;
  }
}
has value

In general, using has_value() is the most explicit and self-explanatory way. Using the bool conversion operator or comparing to std::nullopt can lead to more concise code, but it might be less obvious what the intent is.

Remember, trying to access the value of an empty optional leads to undefined behavior (if using operator* or operator->) or an exception (if using value()). Always check that the optional has a value before trying to access it.

Nullable Values, std::optional and Monadic Operations

A comprehensive guide to using std::optional to represent values that may or may not be present.

Questions & Answers

Answers are generated by AI models and may not have been reviewed. Be mindful when running any code on your device.

When should I use std::optional in C++?
In what situations is it appropriate to use std::optional instead of just regular values or pointers?
std::optional vs pointers in C++
When should I use std::optional instead of a pointer in C++? What are the differences?
Accessing the value in a std::optional
What is the best way to access the value stored in a std::optional? When should I use value() vs operator*?
Using std::optional for class members
How can I use std::optional for members in my C++ classes? Can you provide an example?
Monadic operations with std::optional
Can you explain and provide examples of the monadic operations available for std::optional in C++23?
Using std::optional as a return type
When is it appropriate to use std::optional as a return type for a function?
Performance considerations with std::optional
Are there any performance considerations to keep in mind when using std::optional?
Using std::optional with pointers
Can std::optional be used with pointers? If so, how?
Or Ask your Own Question
Get an immediate answer to your specific question using our AI assistant