Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <string>
- #include <variant>
- // Functor composed of a variable number of other functors
- // Each inherited functor represents a different type pattern
- template<typename ...Types>
- struct match : Types... {
- match(Types... types) : Types{ types }... {}
- using Types::operator()...;
- };
- // Type deduction hint
- template<typename ...Types>
- match(Types...)->match<Types...>;
- void foo() {
- // Variable whose type is one of { int, std::string }
- auto v = std::variant<int, std::string>{ 5 };
- std::visit(match(
- [](int a) {},
- [](const std::string &b) {}
- ), v);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement