Nullable Values using std::optional

Performance considerations with std::optional

Are there any performance considerations to keep in mind when using std::optional?

Illustration representing computer hardware

While std::optional is a very useful tool, there are a few performance considerations to keep in mind:

  1. Size overhead: An std::optional object is typically the size of the contained object plus an additional boolean to track whether the optional contains a value. This means that std::optional may not be suitable for very large objects or in situations where memory is very constrained.
  2. Value construction and destruction: When an std::optional is assigned a value, the value is constructed inside the optional. When the optional is destroyed or reassigned, the value is destroyed. This construction and destruction can have performance implications, especially if the contained type is expensive to construct or destroy.
  3. Accessing values: Accessing the value in an std::optional (using * or ->) requires a branch to check if the optional contains a value. This branch can have a slight performance impact, especially in tight loops.
  4. Monadic operations: The monadic operations (and_then(), or_else(), transform()) are convenient, but they involve function calls and potentially the creation and destruction of temporary std::optional objects, which can have performance implications.

Here's an example illustrating the first two points:

#include <optional>

class ExpensiveClass {
  ExpensiveClass() {
    // expensive initialization
  ~ExpensiveClass() {
    // expensive cleanup

int main() {
  std::optional<ExpensiveClass> opt;

  // This will construct an ExpensiveClass object
  opt = ExpensiveClass();

  // This will destroy the ExpensiveClass object
  opt = std::nullopt;

In most cases, the performance implications of std::optional are minor and outweighed by the benefits in terms of code clarity and safety. However, in performance-critical code or when working with very large objects, it's good to be aware of these considerations.

Answers to questions are automatically generated and may not have been reviewed.

A computer programmer
Part of the course:

Professional C++

Comprehensive course covering advanced concepts, and how to use them on large-scale projects.

Free, unlimited access

This course includes:

  • 124 Lessons
  • 550+ Code Samples
  • 96% Positive Reviews
  • Regularly Updated
  • Help and FAQ
Free, Unlimited Access

Professional C++

Comprehensive course covering advanced concepts, and how to use them on large-scale projects.

Screenshot from Warhammer: Total War
Screenshot from Tomb Raider
Screenshot from Jedi: Fallen Order
Contact|Privacy Policy|Terms of Use
Copyright © 2024 - All Rights Reserved