多个翻译单元中功能模板实例化的标识 [英] Identity of function template instantiation in multiple translation units

查看:203
本文介绍了多个翻译单元中功能模板实例化的标识的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据 cppref inline函数的身份特征具有多个翻译单位如下:

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

...

...

2)每个翻译单元中的地址都相同.

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

3)所有函数定义中的局部函数静态对象为 在所有翻译单元之间共享(它们都引用同一个对象 在一个翻译单元中定义)

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.

推荐答案

我想知道对于没有内联说明符的函数模板实例化是否同样适用.

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

这同样适用于模板.参见§3.2一种定义规则:

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

  1. 一个类类型(第9章),枚举类型(7.2),具有外部链接的内联函数(7.1.2),类模板(第14章),非静态函数可以有多个定义模板(14.5.6),类模板的静态数据成员(14.5.1.3),类模板的成员函数(14.5.1.1)或未指定某些模板参数的模板专门化(14.7, 14.5.5)在程序中,前提是每个定义出现在不同的翻译单元中,并且定义满足以下要求. ... 如果D是模板并且在多个翻译单元中定义,则前述要求应既适用于模板定义(14.6.3)中使用的模板封闭范围的名称,也应适用于从属对象实例化时的名称(14.6.2).如果D的定义满足所有这些要求,则程序应表现为好像有一个D的定义.如果D的定义不满足这些要求,则行为是未定义的.
  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.

这篇关于多个翻译单元中功能模板实例化的标识的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆