Skip to content

Instantly share code, notes, and snippets.

@DieHertz
Last active August 29, 2015 14:07
Show Gist options
  • Save DieHertz/832aed0001537287a706 to your computer and use it in GitHub Desktop.
Save DieHertz/832aed0001537287a706 to your computer and use it in GitHub Desktop.
#include <chrono>
#include <random>
#include <iostream>
template<typename U> void match(const U&) {}
template<typename U, typename Expr> void match(const U&, const Expr& expr) { expr(); }
template<typename U, typename Expr, typename... Args>
void match(const U& u, const U& cond, Expr expr, const Args&... args) {
return u == cond ? expr() : match(u, args...);
}
int main() {
std::mt19937 engine(std::chrono::system_clock::now().time_since_epoch().count());
std::uniform_int_distribution<int> distribution{0, 9};
const auto value(distribution(engine));
std::cout << value << std::endl;
match(value,
0, [] { std::cout << "one\n"; },
1, [] { std::cout << "two\n"; },
[] { std::cout << "some other number\n"; });
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment