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?
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.
精彩评论