2

I have a problem with the following code:

class SymmetryTypes
{
    public:
        enum Type { REAL, COMPLEX, INTEGER, PATTERN, UNINITIALIZED };

        void f() { cout << "SymmetryTypes::f() invoked" << endl; };
};

template <class T>
class EnumBase : public T
{
    public:
        EnumBase() /* : t_(T::UNINITIALIZED) */ { T::f(); }

    private:
        // T::Type t_;
};

int main(int argc, char* argv[])
{
    EnumBase<SymmetryTypes> symmetry;
    return 0;
}

It compiles, but when I uncomment it doesn't. It seems that I can access function members of base class T, but cannot access enum member and its values (also tried typedefs). Do you know why?

Daniel Langr
  • 22,196
  • 3
  • 50
  • 93

1 Answers1

7

T::Type is intended to refer to a type, so you need typename

typename T::Type t_;

If you omit typename, it thinks when it parses the template you are declaring a member whose name is T::Type (and then it errors out at t_ because there cannot be a second name afterwards). Remember it doesn't know at that point what T is yet. A more elaborated explanation can be found here.

The member initializer is fine. The compiler probably got confused by the member declaration being invalid.

Community
  • 1
  • 1
Johannes Schaub - litb
  • 496,577
  • 130
  • 894
  • 1,212