Visibility of template specialization of C++ function

前端 未结 9 686
情书的邮戳
情书的邮戳 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:07

    Brandon: that's what I thought - the specialized function should never be called. Which is true for the second application I mentioned. The first app, however, clearly calls the specialized form even though the specialization is not declared in the header file!

    I mainly seek enlightenment here :-) because the first app is a unit test, and it's unfortunate to have a bug that doesn't appear in the test but in the real app...

    (PS: I have fixed this specific bug, indeed by declaring the specialization in the header; but what other similar bugs might still be hidden?)

    0 讨论(0)
  • 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<int>();
    

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

    0 讨论(0)
  • 2021-01-01 12:10

    As Anthony Williams says, the extern template construct is the correct way to do this, but since his sample code is incomplete and has multiple syntax errors, here's a complete solution.

    fileA.h:

    namespace myNamespace {
      class classA {
        public:
          template <class T> void SomeFunc() { ... }
      };
    
      // The following line declares the specialization SomeFunc<int>().
      template <> void classA::SomeFunc<int>();
    
      // The following line externalizes the instantiation of the previously
      // declared specialization SomeFunc<int>(). If the preceding line is omitted,
      // the following line PREVENTS the specialization of SomeFunc<int>();
      // SomeFunc<int>() will not be usable unless it is manually instantiated
      // separately). When the preceding line is included, all the compilers I
      // tested this on, including gcc, behave exactly the same (throwing a link
      // error if the specialization of SomeFunc<int>() is not instantiated
      // separately), regardless of whether or not the following line is included;
      // however, my understanding is that nothing in the standard requires that
      // behavior if the following line is NOT included.
      extern template void classA::SomeFunc<int>();
    }
    

    fileA.C:

    #include "fileA.h"
    
    template <> void myNamespace::classA::SomeFunc<int>() { ... }
    
    0 讨论(0)
提交回复
热议问题