Using std::optional with pointers

Can std::optional be used with pointers? If so, how?

Yes, std::optional can be used with pointers. This is particularly useful when you want to express that a pointer might be null, without the risk of null pointer dereferences.

Here's an example of how you might use std::optional with a pointer:

#include <iostream>
#include <optional>

class Player {
 public:
  Player(const std::string& name) : name(name) {}
  std::string name;
};

int main() {
  std::optional<Player*> player;

  // Player does not exist yet, player is nullopt
  if (!player) {
    std::cout << "Player does not exist\n";
  }

  // Create an object and assign it to player
  Player real_player("Hero");
  player = &real_player;

  // Player exists, we can access it
  if (player) {
    std::cout << "Player name: "
      << (*player)->name << '\n';
  }

  // Reset player to nullopt
  player = std::nullopt;

  // Player does not exist anymore
  if (!player) {
    std::cout << "Player does not exist\n";
  }
}

This will output:

Player does not exist
Player name: Hero
Player does not exist

When using std::optional with pointers, you need to be careful about the lifetime of the pointed-to object. In the example above, real_player must outlive player, otherwise player will be left pointing to an object that no longer exists.

Also, when accessing the pointer in an std::optional, you need to first dereference the optional (using *) and then dereference the pointer (using -> or *).

Using std::optional with pointers can help make your code more expressive and safer by explicitly representing the possibility of a null pointer.

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?
Checking if a std::optional has a value
What are the different ways to check if a std::optional contains a value?
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?
Or Ask your Own Question
Get an immediate answer to your specific question using our AI assistant