From what I understand, if you have, for example, an std::vector<int> and an std::vector<float>, the compiler creates two classes, one for each type. Thus, although you reduce the amount of code written, you do not reduce executable size (correct me if I’m wrong).
Is the same true even if the type is a pointer? For example, would instantiating an std::vector<SomeClass*> and an std::vector<SomeOtherClass*> necessarily cause the compiler to generate separate code for each of the two instantiations?
The compiler instantiates as many classes from the template as your program uses. Code generated to go in your executable is a slightly different matter, though, from what classes exist in your program.
In practice, most operations on
vectorwill be inlined. So the executable size probably doesn’t change very much according to how many different classes are instantiated from that template because the bulk of the code size is per function call site rather than per distinct class. But as far as it does depend on the number of instantiations,vector<SomeClass*>andvector<SomeOtherClass*>are different classes.If you do an explicit instantiation of
vector, then all the member functions will get generated for the class. You’ll probably see that difference in code size, if you look for it. But normally you don’t explicitly instantiate template classes, and so only the member functions you use are generated.