KnowledgeHub
Questions
Tags
Users
Search
Alex Rivera
|
Logout
Edit Question
Title
Body
In C++, the typename keyword is needed so the compiler can disambiguate between nested types and nested values in templates. However, there are certain situations where no ambiguity is possible, such as when a derived class inherits from a nested class type. template <class T> class Derived : public T::type { }; Here the typename keyword is not required, and is in fact not even allowed. This makes sense, because the context removes the ambiguity. Here, T::type must refer to a type, since you obviously can't inherit from a value. I would think the same thing would hold true for function template parameters. template <class T> void foo(const T::type& v) { } In this case, the context makes it clear that T::type must refer to a type, since a function parameter can't be a value. Yet, the compiler doesn't accept this. It wants const typename T::type& . This seems inconsistent. Why does the language allow the implicit assumption of a nested type in the context of inheritance, but not in the context of function parameters? In both cases there can be no ambiguity, so why the need for typename in one but not the other?
Tags (comma-separated)
Save Edits
Cancel