Identity of function template instantiation in multiple translation units

↘锁芯ラ 提交于 2019-12-23 22:25:34

问题


According to cppref, the identity characteristics of inline functions in multiple translation units are as follows:

...

2) It has the same address in every translation unit.

3) Function-local static objects in all function definitions are shared across all translation units (they all refer to the same object defined in one translation unit)

...

Simply speaking, a singleton identity is implied.

I'm wondering whether the same applies to function template instantiations without the inline specifier.


回答1:


I'm wondering whether the same applies to function template instantiations without the inline specifier.

The same applies to templates. See §3.2 One definition rule:

  1. There can be more than one definition of a class type (Clause 9), enumeration type (7.2), inline function with external linkage (7.1.2), class template (Clause 14), non-static function template (14.5.6), static data member of a class template (14.5.1.3), member function of a class template (14.5.1.1), or template specialization for which some template parameters are not specified (14.7, 14.5.5) in a program provided that each definition appears in a different translation unit, and provided the definitions satisfy the following requirements. ... If D is a template and is defined in more than one translation unit, then the preceding requirements shall apply both to names from the template’s enclosing scope used in the template definition (14.6.3), and also to dependent names at the point of instantiation (14.6.2). If the definitions of D satisfy all these requirements, then the program shall behave as if there were a single definition of D. If the definitions of D do not satisfy these requirements, then the behavior is undefined.


来源:https://stackoverflow.com/questions/32879038/identity-of-function-template-instantiation-in-multiple-translation-units

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!