Skip to content

Instantly share code, notes, and snippets.

@BigRedEye
Created December 13, 2020 23:53
Show Gist options
  • Select an option

  • Save BigRedEye/d2c70d3b2d95af5e152729fad9b3fbd6 to your computer and use it in GitHub Desktop.

Select an option

Save BigRedEye/d2c70d3b2d95af5e152729fad9b3fbd6 to your computer and use it in GitHub Desktop.
#pragma once
#include <utility>
#include <variant>
template <typename... Ts>
struct Overloaded : Ts... {
using Ts::operator()...;
};
template <typename... Ts>
Overloaded(Ts...) -> Overloaded<Ts...>;
template <typename T>
class Matcher {
public:
template <typename U>
Matcher(U&& value) : value_{std::forward<U>(value)} {
}
template <typename F>
decltype(auto) operator<<=(F&& f) {
return std::visit(std::forward<F>(f), std::forward<T>(value_));
}
private:
T&& value_;
};
template <typename U>
Matcher(U &&) -> Matcher<U&&>;
#define match(var) Matcher(var) <<= Overloaded
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment