* Juha Nieminen, on 09.07.2010 13:47:
If they weren't, you would get linker errors when you use the same
template function in more than one compilation unit.
The linker behavior, discarding redundant linker records, is the same yes. But
the C++ concept of "inline" is different and has different rules, in particular
the machine code inlining hint of "inline". Just to invent a name for the common
linker behavior, let's talk about DISCARDABLE definitions.
Then one property of an "inline" routine is that it is DISCARDABLE, and so is an
instantiation of a routine template.
However, an explicit full specialization of a routine template is not
DISCARDABLE. And yes, you do get linker errors if you forget to smack an
"inline" on that! One might rationalize this by saying that it's no longer a
template but just an ordinary routine -- except that it does have enough
memory of its template origins, so to speak, that it can coexist with a really
ordinary routine of the same type and name:
<code>
#include <iostream>
using namespace std;
template< class Type >
void foo( Type const& )
{
cout << "General template" << endl;
}
template<> void foo( double const& )
{
cout << "Fully specialized template" << endl;
}
void foo( double const& )
{
cout << "Completely ordinary routine" << endl;
}
int main()
{
foo( 3.14 );
foo<double>( 3.14 );
}
</code>
OK, that was perhaps a digression.
But anyway, the DISCARDABLE property of template instantiations does not include
the inlining hinting the "inline" does, and so it is misleading to talk about
"inline" for template instantiations.
AFAIK they can't be implicitly declared 'static' either because then
any possible static variables inside the function would get duplicated
for each compilation unit where the function is called (which I think
would be against the specification of template functions).
Right.
(Of course here I'm talking about template function definitions. These
definitions get an implicit 'inline' declaration even without an explicit
'inline' keyword.
No, they don't.
Using the terminology invented above, instantiations of them are DISCARDABLE,
/like/ "inline" routines.
That's all.
Template function *declarations* are a different story,
of course, as "instantiating" them only instantiates a function declaration
rather than a function definition, in which case 'inline' wouldn't make
much sense.)
Unclear what you mean here.
Cheers & hth.,
- Alf