Template specialization and partial specialization

  • Thread starter Thread starter Alfonso Morra
  • Start date Start date
Carl said:

Do I have to define my specializations insitu (in header file), or can I
define them elsewhere (in a .cpp file)?. There seems to be some
confusion in this matter, wrt VC 7.1
 
Alfonso said:
Do I have to define my specializations insitu (in header file), or
can I define them elsewhere (in a .cpp file)?. There seems to be some
confusion in this matter, wrt VC 7.1

All template definitions, specialization or not, must be visible at the
point of use. In practice, that means they must be in a header file.

At present, this is true for all C++ compilers except Comeau, which remains
the only compiler to have a supported implementation of the C++ 'export'
facility. Using 'export', you can place your template deifnitions in a
"cpp" file.

-cd
 
Alfonso said:
Do I have to define my specializations insitu (in header file), or can I
define them elsewhere (in a .cpp file)?. There seems to be some
confusion in this matter, wrt VC 7.1

Declarations of all partial class template and explicit function and
class template specializations should always go in headers (for all
compilers), since the declaration of a specialization must appear before
that specialization is used, otherwise the code has undefined behaviour.
Explicit class template specializations should also be defined in the
header (otherwise they will be incomplete types).

Definitions of members of partial specializations should generally go in
headers (except when your compiler supports "export", which VC does not).

Definitions of explicit function template specializations or members of
explicitly specialized class templates should go in .cpp files (unless
declared inline, or defined in the class body).

I hope that is reasonably clear. Visual C++ implements the "Borland"
model of template instantiation, where definitions of non-explicit
template specializations must always be visible at the point of
instantiation.

Tom
 
Back
Top