Skip to content

Instantly share code, notes, and snippets.

@kybernetyk
Last active August 29, 2015 14:01
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 kybernetyk/b4496d1f1f0f941186e9 to your computer and use it in GitHub Desktop.
Save kybernetyk/b4496d1f1f0f941186e9 to your computer and use it in GitHub Desktop.
/compile with:
// clang++ -std=c++1y -stdlib=libc++ optional.cpp
#include <experimental/optional>
#include <string>
#include <cstdio>
std::experimental::optional<std::string> get_fancy_shit() {
//return std::string("some fancy shit!");
return std::experimental::nullopt;
}
int main(int argc, char *argv[]) {
auto s = get_fancy_shit().value_or("no fancy shit!");
printf("I got: %s\n", s.c_str());
auto o = get_fancy_shit();
if (o) {
printf("We have some fancy shit\n");
} else {
printf("We have no fancy shit\n");
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment