Template definitions outside class body

Yes, it’s OK even without inline. It works the same for ordinary member functions and static variables:

// everything in the header:
template <class T>
class A
{
  static int i;
};

template <class T>
int A<T>::i=0;

Standard quote: (3.2/5)

There can be more than one definition of a class type (Clause 9), enumeration type (7.2), inline function with
external linkage (7.1.2), class template (Clause 14), non-static function template (14.5.6), static data member
of a class template (14.5.1.3), member function of a class template (14.5.1.1), or template specialization for
which some template parameters are not specified (14.7, 14.5.5) in a program provided that each definition
appears in a different translation unit, and provided the definitions satisfy the following requirements …

The requirements basically say the two definitions have to be identical.

It doesn’t work in case of regular classes. There has to be at most one definition in the whole program.

Leave a Comment