Last active
August 29, 2015 14:07
-
-
Save DieHertz/832aed0001537287a706 to your computer and use it in GitHub Desktop.
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 <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