Created
May 22, 2022 07:53
-
-
Save rahulbhadani/57418242c9f663df8c113fb121909cd8 to your computer and use it in GitHub Desktop.
std::optional example 1
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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