Function template specialization format

后端 未结 1 1073
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-13 04:35

What is the reason for the second brackets <> in the following function template:

template<> void doh::operator()<>(int i)

T

相关标签:
1条回答
  • 2020-12-13 05:01

    I've looked it up, and found that it is specified by 14.5.2/2:

    A local class shall not have member templates. Access control rules (clause 11) apply to member template names. A destructor shall not be a member template. A normal (non-template) member function with a given name and type and a member function template of the same name, which could be used to generate a specialization of the same type, can both be declared in a class. When both exist, a use of that name and type refers to the non-template member unless an explicit template argument list is supplied.

    And it provides an example:

    template <class T> struct A {
        void f(int);
        template <class T2> void f(T2);
    };
    
    template <> void A<int>::f(int) { } // non-template member
    template <> template <> void A<int>::f<>(int) { } // template member
    
    int main()
    {
        A<char> ac;
        ac.f(1); //non-template
        ac.f(’c’); //template
        ac.f<>(1); //template
    }
    

    Note that in Standard terms, specialization refers to the function you write using an explicit specialization and to the function generated using instantiation, in which case we have to do with a generated specialization. specialization does not only refer to functions you create using explicitly specializing a template, for which it is often only used.

    Conclusion: GCC gets it wrong. Comeau, with which i also tested the code, gets it right and issues a diagnostic:

    "ComeauTest.c", line 16: error: "void doh::operator()(bool)" is not an entity that can be explicitly specialized template<> void doh::operator()(bool i)

    Note that it isn't complaining about the specialization of the template for int (only for bool), since it doesn't refer to the same name and type: The function type that specialization would have is void(int), which is distinct from the function type of the non-template member function, which is void(bool).

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