Saturday, November 27, 2010

Template Bodies

If your template body is in a cpp file that does not instantiate the template, you will get a linker error if you try to instantiate the
template in another cpp file.


When the compler compiles the file (aka translation unit) that contains the template, it will only instantiate the types of the template that are in that file. These will be the only instantiations in the generated object file. This can seem mysterious, because the template might work for some types and not for others.

This is why it is recommended to put template bodies in the header that defines them.

No comments:

Post a Comment