Shorten the path to a C ++ enum element (using typedef or typename) to use as a template parameter

I have a rather complex object,

MyNamespace::MyClass::MySubStruct

      

which has

enum
{
   ONE = 1,
   TWO = 2
};

      

Now I have another class that has a template parameter

template <unsigned int x> class Foo;

      

I am currently initializing B like this

Foo<MyNamespace::MyClass::MySubStruct::ONE> MyFoo

      

and it works great, but it is too much, especially considering that I am initializing this class about a hundred times.

I would like to write something like:

typedef MyNamespace::MyClass::MySubStruct::ONE  MyONE
Foo<MyOne> MyFoo

      

Naturally this doesn't compile, nor does it declare it as const unsigned int inside the class. How do you do this elegantly?

+3


source to share


2 answers


Enumerators are values, not types. If you only want this specific counter, declare a constant:

const auto MyONE = MyNamespace::MyClass::MySubStruct::ONE;

      



If you need more than just this, it would be possible to add a typedef for MySubStruct

and access the counters through that.

+6


source


ONE

is not a type; this value.

Instead of using, typedef

you can just use a constant:



const auto MyONE = MyNamespace::MyClass::MySubStruct::ONE;

      

Consider also using enum class

instead of enum

.

+2


source







All Articles