Does it? I'm interested in what the C++20 standard (N4860) says about it.
I am aware that gcc/clang/msvc all do not complain if I omit the inline
keyword in such a situation, e.g. code like this
// header that is included in multiple .cpp files:
template <class T>
struct X{
void foo();
};
template <class T>
void X<T>::foo(){}
never gave me any compiler/linker errors (assuming foo
is actually invoked from several TUs). But I fail to find a specific quote in the standard that says I am allowed to omit the inline keyword for the out-of-body definition of foo
. Of course, if the struct above is not templated, then this is an odr violation (as per [11.4.1 p1] and [6.3 p10]). But for a template class, [13.7.1.1 p1] only says that I am allowed to place the definition out of body, without any mention to whether such function is considered inline.
There can be more than one definition of a class type, enumeration type, inline function with external linkage (dcl.inline), inline variable with external linkage (dcl.inline), class template, non-static function template, static data member of a class template, member function of a class template ... that each definition appears in a different translation unit, and provided the definitions satisfy the following requirements...
Later it was changed to "all is odr-use except" basic.def.odr.14
For any definable item D with definitions in multiple translation units, (14.1)
- if D is a non-inline non-templated function or variable, or (14.2)
- if the definitions in different translation units do not satisfy the following requirements,
the program is ill-formed;
...
- If D is a template and is defined in more than one translation unit, the requirements apply both to names from the template's enclosing scope used in the template definition, and also to dependent names at the point of instantiation ([temp.dep]).