3

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.

Alon
  • 1,776
  • 13
  • 31

2 Answers2

7

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?:

Community
  • 1
  • 1
juanchopanza
  • 223,364
  • 34
  • 402
  • 480
4

you have to use typename keyword to make it work:

static void Foo(T t, typename T::B b) {}
                        ^

and enum B has to be public.

typename keyword it is used for specifying that a dependent name in a template definition or declaration is a type. It is synonym for a class in template parameters.

C++ standard states:

A name used in a template declaration or definition and that is dependent on a template-parameter is assumed not to name a type unless the applicable name lookup finds a type name or the name is qualified by the keyword typename.

so unless you state explicitly typename T::B b, compiler will assume T::B b is value type not a type name.

To summarise:

class A {
 public:
  enum B {enum1, enum2};
};

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

int main()
{
  A a;
  Foo(a, a::enum1); 
}

typename

4pie0
  • 29,204
  • 9
  • 82
  • 118