c++templateslinker

Multiple definitions of a non-template class vs. a template class


Why do compilers complain about a non-template class defined in multiple .cpp files but are fine with a template class the definition of which is duplicated across various .cpp files (via inclusion of the .inl file of the class), even if the class is explicitly instantiated in even multiple .cpp files?


Solution

  • Template functions are inline, and inline functions are allowed to be defined in multiple compilation units as long as each definition is identical.