Reputation: 15387
This compiles:
template <typename T> class Parent { public:
enum MyEnum { RED,GREEN,BLUE };
};
class Child : public Parent<int> { public:
using Parent<int>::MyEnum;
int foo() { return GREEN; }
};
void tester() { Child d; d.foo(); }
This doesn't (on gcc, this outputs error: 'GREEN' was not declared in this scope
):
template <typename T> class Parent { public:
enum MyEnum { RED,GREEN,BLUE };
};
template <typename T> class Child : public Parent<T> { public:
using Parent<T>::MyEnum;
int foo() { return GREEN; }
};
void tester() { Child<int> d; d.foo(); }
My question: why? (Also, any suggestions for a workaround?)
Upvotes: 1
Views: 107
Reputation: 1593
For the second code :
using typename Parent<T>::MyEnum;
foo
you need to specify that GREEN is a member of the enum MyEnum like that : int foo() { return MyEnum::GREEN; }
compiled fine for me with gcc and clang with C++11
Live example here
It worked in the first example because MyEnum in the line of the using is not dependent of a template type T. You used explicitly Parent with type int.
Upvotes: 1
Reputation: 206577
The line
using Parent<int>::MyEnum;
has no bearing on being able to use GREEN
in the line
int foo() { return GREEN; }
You can remove the first line and the second line should continue to work.
As for the second example, you can use:
template <typename T> class Child : public Parent<T>
{
public:
using Parent<T>::GREEN;
int foo() { return GREEN; }
};
It's not immediately clear to me why Parent<T>::GREEN
is not automatically available in Child
Upvotes: 0