Visibility of template specialization of C++ function

前端 未结 9 796
情书的邮戳
情书的邮戳 2021-01-01 11:21

Suppose I have fileA.h which declares a class classA with template function SomeFunc(). This function is implemented directly

9条回答
  •  甜味超标
    2021-01-01 12:08

    It is an error to have a specialization for a template which is not visible at the point of call. Unfortunately, compilers are not required to diagnose this error, and can then do what they like with your code (in standardese it is "ill formed, no diagnostic required").

    Technically, you need to define the specialization in the header file, but just about every compiler will handle this as you might expect: this is fixed in C++11 with the new "extern template" facility:

    extern template<> SomeFunc();
    

    This explicitly declares that the particular specialization is defined elsewhere. Many compilers support this already, some with and some without the extern.

提交回复
热议问题