Created
August 30, 2020 07:31
-
-
Save cleoold/e5102c5fcaf4c487b24d6f8f9fb67517 to your computer and use it in GitHub Desktop.
switch case style` std::conditional`-like thing for pattern matching
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
// 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