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?
-
Perhaps you should post the compiler's complaint and the code that generated it, so it's clear what you're asking about. – bames53 Jul 10 '12 at 16:44
3 Answers
The non-template case is because in that scenario your program violates the one definition rule so the linker (not compiler) will complain about multiple definitions.
For templates on the other hand, the language specifies that this must work and the linker sorts out what to do. I'm not 100% sure if an explicit template instantiation should be treated the same as a non-template function though.

- 95,107
- 10
- 109
- 188
-
explicitly instantiating a template in multiple translation units is handled just like implicit instantiations in each TU. – bames53 Jul 10 '12 at 15:49
-
I'm not sure I understand. He asked about classes, and you _can_ (and must) have multiple definitions of a class. – James Kanze Jul 10 '12 at 15:50
Do all compilers complain, always? I've never seen one which did, and the standard doesn't allow it: you're allowed to define a class or a template once in each translation unit, as long as all of the definitions are identical. In fact, you're required to define the class in every translation unit which uses it in a way that requires it to be a complete type. C++ does not have any mechanism for exporting class definitions to other translation units.
Are you sure you're not confusing classes with functions. You're not allowed to define a function more than once, unless it is inline. You still have to define a function template in each translation unit which uses it, and the same rules apply for function templates as for classes and class templates.
Note that if you violate these rules, by defining the function in more than one translation unit, or by the definitions of the classes or templates not being tokenwise identical (after preprocessing, and including name binding), then you have undefined behavior. The compiler (actually the linker) may complain about it, but it's not required: most complain about multiple definitions of a function, but I don't know of any which complain when the class or template definitions differ between translation units.

- 150,581
- 18
- 184
- 329
-
1Defining a non-template classes non-inline member functions in multiple translation units is not allowed. – bames53 Jul 10 '12 at 15:51
-
@bames53 Defining a non-inline function, member or not, in multiple translation units is undefined behavior. That may have been what he meant, but what he asked about was defining a class, not a function. – James Kanze Jul 10 '12 at 16:25
Template functions are inline, and inline functions are allowed to be defined in multiple compilation units as long as each definition is identical.

- 299,747
- 42
- 398
- 622
-
Does it mean, in turn, that marking a function of a template class (or just a template function) with `inline` is totally pointless as it's already qualified as inline? – Desmond Hume Jul 10 '12 at 16:00
-
@DesmondHume, I think so but I usually define my template functions in the class declaration so I'm not sure. – Mark Ransom Jul 10 '12 at 16:26
-
All functions defined inside a class definition are implicitly inline, but I don't believe template functions in general are. There's just a similar rule for template functions allowing them to be defined in multiple translation units. – bames53 Jul 10 '12 at 16:40