Explicit instantiation of template function before definition

Viewed 178

Are explicit instantiations of template functions allowed before the definition of the function, if a declaration occurs first?

E.g., is the following allowed:

// declaration
template <typename T>
void foo(T param);

// explicit instantiation
template void foo<int>(int);

// definition of primary template
template <typename T>
void foo(T param) {}

In general it seems to compile, but of course that is only a mild indication as to whether it is allowed or not.

1 Answers

There is nothing that explicitly forbids it in [temp.explicit]. Furthermore, there is an explicit statement ([temp.explicit#5]):

A declaration of a function template, a variable template, a member function or static data member of a class template, or a member function template of a class or class template shall precede an explicit instantiation of that entity.

If a definition was required, this paragraph would be different (i.e., it will require a definition), thus your code seems legal.

Related