Visibility of template specialization of C++ function

前端 未结 9 685
情书的邮戳
情书的邮戳 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 11:52

    Per the specs, your specialized function template should never be called outside fileA.C, unless you export the template definition, which no compiler (except Comeau) currently supports (or has it planned for the forseeable future).

    On the other hand, once the function template is instantiated, there is a function visible to the compiler that is no longer a template. GCC may re-use this definition across different compiler units because the standard states that each template shall only be instantiated once for a given set of type arguments [temp.spec]. Still, since the template is not exported, this should be limited to the compilation unit.

    I believe that GCC may expose a bug here in sharing its list of instantiated templates across compilation units. Normally, this is a reasonable optimization but it should take function specializations into account which it doesn't seem to do correctly.

    0 讨论(0)
  • 2021-01-01 11:53

    I had the same problem with gcc4, here is how i solved it. It was more simple a solution than what i was lead to believe by previous comments. The previous posts ideas were correct but their syntax didn't work for me.

    
        ----------header-----------------
        template < class A >
        void foobar(A& object)
        {
          std::cout << object;
        }
    
        template <> 
        void foobar(int);
    
        ---------source------------------
        #include "header.hpp"
    
        template <>
        void foobar(int x)
        {
          std::cout << "an int";
        }
    
    
    0 讨论(0)
  • 2021-01-01 11:56

    Unless the specialized template function is also listed in the header file, the other application will have no knowledge of the specialized version. The solution is the add SomeFunc<int>() to the header as well.

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

    Have you added a prototype with parameters to your header file?

    I mean is there somewhere in fileA.h

    template<> SomeFunc<int>();
    

    If not that's probably the reason.

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

    In Microsoft C++, I did an experiment with inline functions. I wanted to know what would happen if I defined incompatible versions of a function in different sources. I got different results depending on whether I was using a Debug build or a Release build. In Debug, the compiler refuses to inline anything, and the linker was linking the same version of the function no matter what was in scope in the source. In Release, the compiler inlined whichever version had been defined at the time, and you got differing versions of the function.

    In neither case were there any warnings. I kind of suspected this, which is why I did the experiment.

    I assume that template functions would behave the same, as would other compilers.

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

    @[anthony-williams],

    are you sure you're not confusing extern template declarations with extern template instantiations? From what I see, extern template may only be used for explicit instantiation, not for specialization (which implies implicit instantiation). [temp.expl.spec] doesn't mention the extern keyword:

    explicit-specialization:
        template < > declaration

    0 讨论(0)
提交回复
热议问题