TITLE: simple explaination of typename (Newsgroups: comp.lang.c++.moderated, 12 Sep 98) SHIVA: Shiva > > According to stroustrup 3E, >a vector class should look like this > >template > > class vector { >public: > typedef typename _A::size_type size_type; > typedef typename _A::difference_type difference_type; >.... >}; > >but I checked Dimkumware's STL, the typename is missing. > typedef _A::size_type size_type; > typedef _A::difference_type difference_type; > > >Stroustrup says without the typename keyword, the compiler has no way >of knowing if a member of a template argument type is a typename. > >So, how does Dimkumware's STL work without this ?? CLAMAGE: clamage@Eng.Sun.COM (Steve Clamage) The typename keyword is a recent addition to C++. Not all compilers support it yet, and of those that do, some don't require its use. The keyword was added to allow better error reporting for templates. It isn't really necessary to allow template use. Example: int c; template< class T > void func() { T::C * c; // what is this? ... } Until func is instantiated on some type T, it isn't possible to make sense of the marked line. If T::C is a type, c is an uninitialized pointer to that type. If T::C is a static data member of class T, it multiplies that object by the global int c. Further uses of c in func can't be understood until this line is resolved as to meaning, apart from any other semantic restrictions that may apply. With the requirement of using typename to indicate a type, we know that the marked line is not a declaration of a local variable c. If for example func later has the statment c = new T::C; the compiler can mark the error in the template definition itself, since it cannot be correct for any instantiation of func. Error messages on later instantiations of func can be supressed, since func itself is unconditionally invalid. Without the typename requirement, the compiler has no choice but to wait for instantiations, and issue possibly many copies of the same error message for each instantiation.