[c++] std::experimental::optional

The std::experimental::optional is a template class introduced in the C++17 standard library. It is part of the C++17 Library Fundamentals Technical Specification 2 (TS2) and provides a way to represent optional (nullable) objects.

Introduction to std::experimental::optional

The std::experimental::optional is a container that may or may not hold a value. It is useful for scenarios where a value might not be available, and it eliminates the need for null pointers or sentinel values.

Syntax

To use std::experimental::optional, include the <optional> header file and declare an optional object with the desired type.

#include <optional>
std::experimental::optional<int> optInt;

Operations

Example Usage

#include <optional>
#include <iostream>

int main() {
  std::experimental::optional<int> optInt;
  
  if (optInt.has_value()) {
    std::cout << "Value is present: " << optInt.value() << std::endl;
  } else {
    optInt = 42;
    std::cout << "Value is now present: " << optInt.value() << std::endl;
  }
  
  int val = optInt.value_or(-1);
  std::cout << "Value or default: " << val << std::endl;
  return 0;
}

Conclusion

std::experimental::optional provides a safer and more expressive way to represent optional values in C++. It eliminates many of the pitfalls associated with using null pointers, making code more robust and easier to maintain.

For further information and examples, please refer to the C++17 standard and relevant documentation.

For more information, refer to the C++ Reference for std::experimental::optional