Context
We develop a templated settings system class, that will be part of an API we expose to users of different architectures (fewer words : we should not rely on compiler-specific behaviors)
The generalized code would look like :
In the header
namespace Firm
{
// Class definition
class A
{
template<class T>
void foo(T* aParam);
};
// Non specialized template definition
template<class T>
void A::foo(T* aParam)
{
//...
}
// Declaration of a specialization
template<>
void A::foo<int>(int* aParam);
} // namespace
In the CPP file
namespace Firm
{
// Definition of the specialized member function
template<>
void A::foo<int>(int* aParam)
{
//...
}
} // namespace
Questions
Everything runs fine with gcc 4.x. (i.e. Different compilation units use the specialized methods when appropriate.) But I feel uncomfortable since I read the following entry :
Visibility of template specialization of C++ function
Accepted answer states, if I correctly understand it, that it is an error if the definition of the specialization of a template method is not visible from call site. (Which is the case in all compilation units that are not the CPP file listed above but include the header)
I cannot understand why a declaration would not be enough at this point (declaration provided by the header) ?
If it really is an error, is there a correct way to define the specialization for it to be :
- non-inline
- usable in any compilation unit that includes the header (and links with the corresponding .obj) ?