what is elvis operator
What is Elvis Operator (?:)
March 8, 2022

March 1, 2022

C++17 std::optional

C++17 introduces a concept called std::optional. The class template std::optional manages optional contained value, a value that may or may not be present. It is commonly used in the return values of the functions that may fail.
First function returns an optional string, that is the function may return a string or may not return anything. Hence, it is optional.
Second function returns the string if the variable b is true else it returns a nullopt.

std::optional < std::string biss(bool b)
{
    if (b)
    {
        return "Hello Biss";
    }
    return {};
}
auto biss2(bool b)
{
    return b ? std::optional<std::string>{
                   "Hello Biss"
    } std::nullopt;
}
C++17 std::optional
This website uses cookies to improve your experience. By using this website you agree to our Data Privacy Statement
Read more