std::optional represents a value that may or may not be present.

optional An optional value makes the missing-value case visible instead of using a special number.

Optional Values

raw
optional_values.cpp
Replay: real traced execution (multi-file project)
#include <iostream>
#include <optional>
#include <string>

std::optional<int> readCount(const std::string& text) {
    if (text == "none") {
        return std::nullopt;
    }
    return std::stoi(text);
}

int main() {
    std::string raw = "42";

    std::optional<int> count = readCount(raw);

    if (count.has_value()) {
        std::cout << "count=" << count.value() << std::endl;
    } else {
        std::cout << "count=missing" << std::endl;
    }

    std::cout << "fallback=" << count.value_or(0) << std::endl;
    return 0;
}
#include <iostream>
#include <optional>
#include <string>

std::optional<int> readCount(const std::string& text) {
    if (text == "none") {
        return std::nullopt;
    }
    return std::stoi(text);
}

int main() {
    std::string raw = "none";

    std::optional<int> count = readCount(raw);

    if (count.has_value()) {
        std::cout << "count=" << count.value() << std::endl;
    } else {
        std::cout << "count=missing" << std::endl;
    }

    std::cout << "fallback=" << count.value_or(0) << std::endl;
    return 0;
}
#include <iostream>
#include <optional>
#include <string>

std::optional<int> readCount(const std::string& text) {
    if (text == "none") {
        return std::nullopt;
    }
    return std::stoi(text);
}

int main() {
    std::string raw = "7";

    std::optional<int> count = readCount(raw);

    if (count.has_value()) {
        std::cout << "count=" << count.value() << std::endl;
    } else {
        std::cout << "count=missing" << std::endl;
    }

    std::cout << "fallback=" << count.value_or(0) << std::endl;
    return 0;
}
  1. raw ← 42

    12int main() {13    std::string raw→ 42 = "42"; //@raw="none", "7"1415    std::optional<int> count = readCount(raw);
  2. std::optional<int> readCount(const std::string& text)

    5std::optional<int> readCount(const std::string& text42) {6    if (text == "none") {7        return std::nullopt;8    }9    return std::stoi(text42);10}
  3. std::optional<int> count = readCount(raw);

    15std::optional<int> count = readCount(raw);
  4. if (count.has_value())

    17if (count.has_value()) {18    std::cout << "count=" << count.value() << std::endl;19} else {
    outputcount=42
  5. std::cout << "fallback=" << count.value_or(0) << std::endl;

    23    std::cout << "fallback=" << count.value_or(0) << std::endl;24    return 0;25}
    outputfallback=42
  1. raw ← none

    12int main() {13    std::string raw→ none = "none";1415    std::optional<int> count = readCount(raw);
  2. std::optional<int> readCount(const std::string& text)

    5std::optional<int> readCount(const std::string& textnone) {6    if (text == "none") {
  3. if (text == "none")

    5std::optional<int> readCount(const std::string& text) {6    if (textnone == "none") {7        return std::nullopt;8    }
  4. std::optional<int> count = readCount(raw);

    15std::optional<int> count = readCount(raw);
  5. else

    18    std::cout << "count=" << count.value() << std::endl;19} else {20    std::cout << "count=missing" << std::endl;21}
    outputcount=missing
  6. std::cout << "fallback=" << count.value_or(0) << std::endl;

    23    std::cout << "fallback=" << count.value_or(0) << std::endl;24    return 0;25}
    outputfallback=0
  1. raw ← 7

    12int main() {13    std::string raw→ 7 = "7";1415    std::optional<int> count = readCount(raw);
  2. std::optional<int> readCount(const std::string& text)

    5std::optional<int> readCount(const std::string& text7) {6    if (text == "none") {7        return std::nullopt;8    }9    return std::stoi(text7);10}
  3. std::optional<int> count = readCount(raw);

    15std::optional<int> count = readCount(raw);
  4. if (count.has_value())

    17if (count.has_value()) {18    std::cout << "count=" << count.value() << std::endl;19} else {
    outputcount=7
  5. std::cout << "fallback=" << count.value_or(0) << std::endl;

    23    std::cout << "fallback=" << count.value_or(0) << std::endl;24    return 0;25}
    outputfallback=7