example3_default_values.cpp

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
#include <iostream>
#include <optional>
#include <string>

int main() {
    std::optional<std::string> opt_str;
    
    std::cout << "Value or default: " << opt_str.value_or("Default") << std::endl;
    
    opt_str = "Hello";
    std::cout << "Value or default: " << opt_str.value_or("Default") << std::endl;
    
    return 0;
}
Back to optional