C++ template dispatch with enum variables

Viewed 98

I have a function with a template of two enum variables. Is there any way to dispatch the function f with different combinations of enumA and enumB? I do not want to write too much if. In this case, I may only need to write 4 branches, but in actual case, I need to write tens branches.

#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <cassert>
using namespace std;

enum EnumA
{
    enumA0,
    enumA1
};
enum EnumB
{
    enumB0,
    enumB1
};

template <EnumA enumA, EnumB enumB>
void f()
{
    if (enumA == enumA0)
    {
        if (enumB == enumB0)
        {
            // something here
            return;
        }
        else if (enumB == enumB1)
        {
            // something here
            return;
        }
        else
        {
            assert(false);
        }
    }
    else if (enumA == enumA1)
    {
        if (enumB == enumB0)
        {
            // something here
            return;
        }
        else if (enumB == enumB1)
        {
            // something here
            return;
        }
        else
        {
            assert(false);
        }
    }
}

int main()
{
    int a, b;
    cin >> a >> b;
    const auto enumA = a < 0 ? enumA0 : enumA1;
    const auto enumB = b < 0 ? enumB0 : enumB1;
    f<enumA, enumB>(); // Is there a way to dispatch this with different enumA and enumB combinations?
    return 0;
}
0 Answers
Related