Where should the definition of an explicit specialization of a class template be placed in C++?

后端 未结 2 1044
既然无缘
既然无缘 2021-01-11 17:01

According to [temp.spec]/5:

For a given template and a given set of template-arguments,

  • ...

  • an explicit specialization s

2条回答
  •  心在旅途
    2021-01-11 18:01

    This is a definition (the specialization will be instantiated), not a declaration, it probably ought to go in a specific source file (*.cpp):

    template <> struct S;
    

    Note: it doesn't "hurt" to have this in every translation unit... other than the time it takes to instantiate by the compiler, and the bloat for the object files (*.o or *.obj).

    This is a declaration (the specialization will not be instantiated), and is okay to put in a header file (*.h):

    extern template <> struct S;
    

    The declaration requires C++11 or later.

提交回复
热议问题