Local class as Template Argument - Point of Instantiation

Viewed 51

I am trying to wrap my head around example from TC++PL, 4th edition (p.751).

void fff()
{
    struct S { int a,b; };
    vector<S> vs;
    // ...
}

From the discussion, it follows that the point of instantiation of vector<S> is just before the definition of void fff(). However, struct S is not visible outside void fff(). What is proper way to think of the instantiation point with local classes as template arguments?

1 Answers

What is meant with instantiation is template instantiation in this case.

The compiler creates a vector class definition of vector just before fff so it can be reused if another instantiation needs to be done with S later on. It does not create an instance of vector at that point.

Yes, we can't access S from outside fff but the compiler knows about it and create a global template so it can be reused.

see

https://en.cppreference.com/w/cpp/language/class_template

Related