Templates are compiled in two phases:
At definition time (no instantiation yet) the template code itself is checked for correctness (ignoring template parameters) for example: syntax errors, unknown names, etc.
At instantiation time the template code is checked again, for ex. all parts that depend on template parameters are checked.
Note: If you just define a template, but don't use it, then only the 1. phase will be checked.
for example in your case:
template <typename T>
class vector
{
T* data, // 1. Phase check: compile error: missing semicolon
~~~
};
int main()
{
vector<int> a; // 2. Phase check: Instantiation: T replaced with int and template code rechecked.
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…