An "ordinary" function, when defined and used exclusively in a single translation unit is declared and defined like this:
// implementation.cpp
static void fun(int arg) { /* implementation */ }
alternatively you could drop the static
keyword and wrap the function in an unnamed namespace. But failing to do one of the above can lead to ODR violations: if a different translation unit also has a declaratino/definition of fun
(with the same arguments) the liker may silently have to discard one of the implementations to keep a single definition (in which case you'd better have the exact same definition spelled out for both, or you'll see unexpected behaviour).
As described above, the problem is more "implementation oriented", but even the standard would urge you to protect against such side effects using static
or namespace { /**/ }
.
The question is what happens when fun
is a function template? Should I also require wrapping it in an unnamed namespace or declaring it as static inside a translation unit?
template <class T>
/* static ? */ void fun(T arg) { /*...*/ }
In cppreference it is mentioned that if the function is inline
it's ok obviously (requirements for single definition are only posed for non-inline functions) so no ODR worries there:
One and only one definition of every non-inline function or variable that is odr-used (see below) is required to appear in the entire program
We know that function templates are inlined but is this enough to guarantee ODR correctness?