Skip to content

Instantly share code, notes, and snippets.

@naveenspace7
Created August 16, 2020 18:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save naveenspace7/2be0fe067bd87017211999f64f6dba69 to your computer and use it in GitHub Desktop.
Save naveenspace7/2be0fe067bd87017211999f64f6dba69 to your computer and use it in GitHub Desktop.
std::optional usage
// 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