Specialized template function with deleted “general” case fails to compile with g++ <=4.8.0 and clang++

前端 未结 1 1288
终归单人心
终归单人心 2020-12-03 17:28

Compiling a project with an older version of g++ (4.8.0, MinGW) I found that this code fails to compile:

template
void foo() = delete;

tem         


        
相关标签:
1条回答
  • 2020-12-03 17:36

    I don't know if the following will be enlightening but I found defect report 941: Explicit specialization of deleted function template with status C++11 that states the following (Emphasis Mine):

    According to 14.7.3 [temp.expl.spec] paragraph 1, only non-deleted function templates may be explicitly specialized. There doesn't appear to be a compelling need for this restriction, however, and it could be useful to forbid use of implicitly-instantiated specializations while still allowing use of explicitly-specialized versions.

    Proposed resolution (February, 2010):

    Change 14.7.3 [temp.expl.spec] paragraph 1 as follows:

    An explicit specialization of any of the following:

    non-deleted function template

    class template

    non-deleted member function of a class template

    static data member of a class template

    member class of a class template

    member class template of a class or class template

    non-deleted member function template of a class or class template

    can be declared...

    Now the current state of the draft standard N4527 is 14.7.3 Explicit specialization [temp.expl.spec]:

    1 An explicit specialization of any of the following:

    (1.1) — function template

    (1.2) — class template

    (1.3) — variable template

    (1.4) — member function of a class template

    (1.5) — static data member of a class template

    (1.6) — member class of a class template

    (1.7) — member enumeration of a class template

    (1.8) — member class template of a class or class template

    (1.9) — member function template of a class or class template

    ...

    So I guess:

    template<typename T>
    void foo() = delete;
    
    template<>
    void foo<int>(){}
    
    int main() {
        foo<int>();
        return 0;
    }
    

    Is C++11 standard compatible code and should be accepted.

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