Templated enum types based on template parameters

c++, enums, templates

Solution

You need to tell the compiler that `T::B` is a type, because it is a dependent name, and is assumed to be a non-type by default.

template<typename T>
static void Foo(T t, typename T::B b) {}
//                   ^^^^^^^^

You should also make the enum public. This code sample works:

class A {
 public:
  enum B {x, y, z};
};

template<typename T>
static void Foo(T t, typename T::B b) {}

int main()
{
  Foo(A(), A::x); // OK
}

For an in-depth explanation, see Where and why do I have to put the “template” and “typename” keywords?:

Problem

I was wondering how to do this: Say I have a class A, with enum B inside it ``` class A { enum B { }; }; ``` And I'd like to create a function that takes A as a template and then assumes A has an enum B type and receives its val as a parameter? I tried something like: ``` template<typename T> static void Foo(T t, T::B b) {} ``` But that didn't work.. anyone has an idea? Thanks.

Original source

Related problems