Skip to content

Instantly share code, notes, and snippets.

@cleoold
Created August 30, 2020 07:31
Show Gist options
  • Save cleoold/e5102c5fcaf4c487b24d6f8f9fb67517 to your computer and use it in GitHub Desktop.
Save cleoold/e5102c5fcaf4c487b24d6f8f9fb67517 to your computer and use it in GitHub Desktop.
switch case style` std::conditional`-like thing for pattern matching
// match case for c++11
// avoids nested brackets as in std::conditional
template<bool Cond, class Tp>
struct Case {};
template<class ...Cases>
struct Match;
template<class Case_1, class ...Cases>
struct Match<Case_1, Cases...> : Match<Cases...> {};
template<class Tp, class ...Cases>
struct Match<Case<true, Tp>, Cases...> { typedef Tp type; };
template<>
struct Match<> {};
template<class ...Cases>
using Match_t = typename Match<Cases...>::type;
// match case
// a small test
int main() {
using haha = Match_t<
Case<false, float>,
Case<false, double>,
Case<true, short>,
Case<true, int>
>;
static_assert(std::is_same<haha, short>::value);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment