-
-
Save naveenspace7/2be0fe067bd87017211999f64f6dba69 to your computer and use it in GitHub Desktop.
std::optional usage
This file contains 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
// Compile with the command: $ g++ optional_demo.cpp -o out -std=c++17 | |
#include <iostream> | |
#include <optional> | |
#include <fstream> | |
using namespace std; | |
optional<string> getFileData(const string& filePath) | |
{ | |
fstream fstrm(filePath); | |
if (fstrm) | |
{ | |
return {"hello naveen, you got your file constructed"}; | |
} | |
return {}; | |
} | |
int main() | |
{ | |
optional<string> fileContent = getFileData("confi.txt"); | |
if (fileContent) | |
{ | |
// cout << *fileContent << endl; | |
cout << fileContent.data() << endl; | |
} | |
else | |
{ | |
cout << "there is no file, sorry" << endl; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment