C++ FAQ Celebrating Twenty-One Years of the C++ FAQ!!!
(Click here for a personal note from Marshall Cline.)
Section 35:
35.1 What's the idea behind templates?
35.2 What's the syntax / semantics for a "class template"?
35.3 What's the syntax / semantics for a "function template"?
35.4 How do I explicitly select which version of a function template should get called?
35.5 What is a "parameterized type"?
35.6 What is "genericity"?
35.7 My template function does something special when the template type T is int or std::string; how do I write my template so it uses the special code when T is one of those specific types?
35.8 Huh? Can you provide an example of template specialization that doesn't use foo and bar?
35.9 But most of the code in my template function is the same; is there some way to get the benefits of template specialization without duplicating all that source code?
35.10 All those templates and template specializations must slow down my program, right?
35.11 So templates are overloading, right?
35.12 Why can't I separate the definition of my templates class from its declaration and put it inside a .cpp file?
35.13 How can I avoid linker errors with my template functions? Updated!
35.14 How does the C++ keyword export help with template linker errors? Updated!
35.15 How can I avoid linker errors with my template classes? Updated!
35.16 Why do I get linker errors when I use template friends?
35.17 How can any human hope to understand these overly verbose template-based error messages?
35.18 Why am I getting errors when my template-derived-class uses a nested type it inherits from its template-base-class?
35.19 Why am I getting errors when my template-derived-class uses a member it inherits from its template-base-class?
35.20 Can the previous problem hurt me silently? Is it possible that the compiler will silently generate the wrong code?
35.21 How can I create a container-template that allows my users to supply the type of the underlying container that actually stores the values?
35.22 Follow-up to previous: can I pass in the underlying structure and the element-type separately?
35.23 Related: all those proxies must negatively reflect on the speed of my program. Don't they?
[35.18] Why am I getting errors when my template-derived-class uses a nested type it inherits from its template-base-class?

Perhaps surprisingly, the following code is not valid C++, even though some compilers accept it:

template<typename T>
class B {
public:
  class Xyz { ... };   type nested in class B<T>
  typedef int Pqr;     type nested in class B<T>
};

template<typename T>
class D : public B<T> {
public:
  void g()
  {
    Xyz x;   bad (even though some compilers erroneously (temporarily?) accept it)
    Pqr y;   bad (even though some compilers erroneously (temporarily?) accept it)
  }
};
This might hurt your head; better if you sit down.

Within D<T>::g(), name Xyz and Pqr do not depend on template parameter T, so they are known as a nondependent names. On the other hand, B<T> is dependent on template parameter T so B<T> is called a dependent name.

Here's the rule: the compiler does not look in dependent base classes (like B<T>) when looking up nondependent names (like Xyz or Pqr). As a result, the compiler does not know they even exist let alone are types.

At this point, programmers sometimes prefix them with B<T>::, such as:

template<typename T>
class D : public B<T> {
public:
  void g()
  {
    B<T>::Xyz x;   bad (even though some compilers erroneously (temporarily?) accept it)
    B<T>::Pqr y;   bad (even though some compilers erroneously (temporarily?) accept it)
  }
};
Unfortunately this doesn't work either because those names (are you ready? are you sitting down?) are not necessarily types. "Huh?!?" you say. "Not types?!?" you exclaim. "That's crazy; any fool can SEE they are types; just look!!!" you protest. Sorry, the fact is that they might not be types. The reason is that there can be a specialization of B<T>, say B<Foo>, where B<Foo>::Xyz is a data member, for example. Because of this potential specialization, the compiler cannot assume that B<T>::Xyz is a type until it knows T. The solution is to give the compiler a hint via the typename keyword:
template<typename T>
class D : public B<T> {
public:
  void g()
  {
    typename B<T>::Xyz x;   good
    typename B<T>::Pqr y;   good
  }
};