Specialization of template function after point of use will break the compilation

前端 未结 1 1412
长情又很酷
长情又很酷 2021-01-18 20:18

Consider next example :

#include 

template< int a >
void foo();

int main(int argn, char* argv[])
{
    foo<1>();
}

template<         


        
相关标签:
1条回答
  • 2021-01-18 20:29

    I think that's undefined behavior according to the standard. There are no restrictions on what a toolchain can do in cases of UB, generating a compiler error is one of the friendlier possibilities.


    Section [temp.spec], 14.7p5 says

    For a given template and a given set of template-arguments,

    • an explicit instantiation definition shall appear at most once in a program,
    • an explicit specialization shall be defined at most once in a program (according to 3.2), and
    • both an explicit instantiation and a declaration of an explicit specialization shall not appear in a program unless the explicit instantiation follows a declaration of the explicit specialization.

    An implementation is not required to diagnose a violation of this rule.

    Section [temp.expl.spec] 14.7.3p6 says:

    If a template, a member template or a member of a class template is explicitly specialized then that specialization shall be declared before the first use of that specialization that would cause an implicit instantiation to take place, in every translation unit in which such a use occurs; no diagnostic is required.


    Your program violates these requirements.

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