内联函数链接 [英] Inline function linkage

查看:284
本文介绍了内联函数链接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不能理解以下行为:一个头包含一些基本类型,另一个头,其中我在几个函数中使用这些类型。之后,我开始根据我定义的类型和函数构建类。在函数头中如果我留下以下签名:

I can't make sense of the following behavior: one header with some basic types, and another header in which I use these types in several functions. Afterward I started constructing classes based on my defined types and functions. In the function header if I leave the following signature:

void whateverFunction(parameters)

链接器指出,有多个definitionsFunction的定义。现在如果改为:

The linker points out that there are multiple definitions of whateverFunction. Now if change it to:

inline void whateverFunction(parameters)

连接问题消失了,所有编译和链接都很好。我知道有关内联的是,它替换每个函数调用它的代码,而不是一个非常暗,所以我的问题是:

the linkage problem is gone and all compiles and links well. What I know concerning inline is that it replaces every function call with it's code other than that it's a pretty dark, so my question is:

在C ++中处理内联函数

推荐答案

当标题中的函数不是内联函数时,函数(例如在多个翻译单元中)违反ODR规则。

When the function in the header is not inline, then multiple definitions of this function (e.g. in multiple translation units) is a violation of ODR rules.

内联函数默认具有外部链接。因此,由于ODR规则(如下所述),这样的多个定义(例如在多个翻译单元中)是:

Inline functions by default have external linkage. Hence, as a consequence of ODR rules (given below), such multiple definitions (e.g. in multiple translation units) are Okay:


$ 3.2 / 5-类类型可以有多个
定义(第9条),
枚举类型(7.2), inline
带外部链接的函数

(7.1.2),类模板(子句14),
非静态函数模板(14.5.6),
类模板的静态数据成员
(14.5.1.3 ),类
模板(14.5.1.1)的成员函数或模板
特殊化,其中未指定模板
参数(14.7,
14.5.5)程序提供了每个定义出现在不同的
翻译单元中,并且提供
定义满足以下
需求。给定这样的实体
命名D定义在多个
翻译单元,然后

$3.2/5- "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. Given such an entity named D defined in more than one translation unit, then

- D的每个定义应由相同序列的令牌组成
;和[...]

— each definition of D shall consist of the same sequence of tokens; and [...]

链接器如何处理内联函数是一个非常实现级别的细节。只要知道实现在ODR规则的限制内接受这样的多个定义

How the linker treats inline functions is a pretty much implementation level detail. Suffice it to know that the implementation accepts such mulitple defintions within the limitations of ODR rules

请注意,如果头中的函数声明被改为'static inline .... ',那么内联函数显式地具有内部链接,并且每个翻译单元具有它自己的静态内联函数的副本。

Note that if the function declaration in header is changed to 'static inline....', then the inline function explicitly has internal linkage and each translation unit has it's own copy of the static inline function.

这篇关于内联函数链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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