Skip to content

Instantly share code, notes, and snippets.

@DieHertz
Last active August 29, 2015 14:07
Show Gist options
  • Save DieHertz/1db611d78b6ab73f79ea to your computer and use it in GitHub Desktop.
Save DieHertz/1db611d78b6ab73f79ea to your computer and use it in GitHub Desktop.
#include <chrono>
#include <random>
#include <iostream>
template<typename T> struct match_impl {
template<typename F> match_impl& operator()(const T& cond, F f) {
if (value == cond) f();
return *this;
}
const T& value;
};
template<typename T> match_impl<T> match(const T& value) {
return match_impl<T>{value};
}
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"; });
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment