Skip to content

Instantly share code, notes, and snippets.

@rahulbhadani
Created May 22, 2022 07:53
Show Gist options
  • Save rahulbhadani/57418242c9f663df8c113fb121909cd8 to your computer and use it in GitHub Desktop.
Save rahulbhadani/57418242c9f663df8c113fb121909cd8 to your computer and use it in GitHub Desktop.
std::optional example 1
#include <string>
#include <functional>
#include <iostream>
#include <optional>
std::optional<std::string> getcar(std::string value)
{
if (value=="Toyota")
{
return "I got toyota";
}
return {};
}
int main()
{
std::cout << "getcar returns " << getcar("Toyota").value_or("empty") << '\n';
std::cout << "getcar returns " << getcar("Nissan").value_or("empty") << '\n';
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment