Why does explicit template instantiation not break ODR?

前端 未结 2 2035
太阳男子
太阳男子 2020-12-11 18:58

This question arised in the context of this answer.

As I would expect, this translation unit does not compile:

template  int getNum()          


        
2条回答
  •  心在旅途
    2020-12-11 19:52

    Eureka! I finally fall on the relevant paragraph, [temp.spec]/5

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

    • (5.1) an explicit instantiation definition shall appear at most once in a program,

    • (5.2) an explicit specialization shall be defined at most once in a program, as specified in [basic.def.odr], and

    • (5.3) 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.

    So explicit template instantiation definition (not implicit instantiation) can cause ODR violation, no diagnostic required (and at least gcc and clang - ld toolchains do not produce diagnostic)

提交回复
热议问题