Identity of function template instantiation in mul

2019-07-08 17:07发布

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条回答
Melony?
2楼-- · 2019-07-08 17:46

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.
查看更多
登录 后发表回答