Pass anonymous enum as function argument

Viewed 495

Is it possible to pass anonymous enum as a function parameter? Something like that:

class Foo
{
    enum
    {
        One,
        Two,
        Three
    };
};

void Function( /* ??? */ e)
{
    switch (e)
    {
        case Foo::One: // do stuff...
        case Foo::Two: // ...
    }
}

Solution attempt:

I was trying to determine what's the type of Foo::One by using auto and checking deduced type:

auto u = Foo::One;

But it turned out to be Foo<anonymous enum> so I can't really use that in code.

2 Answers

I found a possible solution. Ugly, but works:

void Function(decltype(Foo::One) e) {}

You could turn the function into a function template like this:

template <class Enum> void Function(Enum e)
{
    switch (e)
    {
        case Foo::One: // do stuff...
        case Foo::Two: // ...
    }
}

It can be instantiated and called via

Function(Foo::One);
Related