Avert
Avert

Reputation: 445

Pass anonymous enum as function argument

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.

Upvotes: 3

Views: 871

Answers (2)

lubgr
lubgr

Reputation: 38287

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);

Upvotes: 2

Avert
Avert

Reputation: 445

I found a possible solution. Ugly, but works:

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

Upvotes: 3

Related Questions