Because Graph
is a class template not an ordinary class.
Class templates define a class where the types of some of the variables, return types of methods, and/or parameters to the methods are specified as parameters.
Hence by using Graph<std::size_t > g;
you are using one of the class template instantiation which has size_t
as a type parameter.
You can use Graph<int > g
too and so on.
An addition:
When the compiler encounters template method definitions, it performs syntax
checking ony, but doesn’t actually compile the templates.
Let us write the template
template<typename T>
class MyClass
{
T memberVar{};
};
Only when the compiler encounters an instantiation of the template, such as MyClass<int> myObj
, it writes code for an int
version of the MYClass
template by replacing each T
in the class template definition with int
and so on.