内联函数在不同翻译单元中的不同实现 [英] Different implementations of inline functions in different translation units

查看:129
本文介绍了内联函数在不同翻译单元中的不同实现的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

C ++标准对此进行了说明,因为它适用于内联函数(重点是我的):

The C++ standard says this about ODR, as it applies to inline functions (emphasis mine):

3.2一个定义规则

3每个程序应准确地包含该程序中使用的每个非内联函数或变量的一个定义;无需诊断.该定义可以显式出现在程序中,可以在标准库或用户定义的库中找到,或者(如果适用)可以隐式定义(请参见12.1、12.4和12.8). 内联函数应在使用过的每个翻译单元中定义.

3 Every program shall contain exactly one definition of every non-inline function or variable that is odr-used in that program; no diagnostic required. The definition can appear explicitly in the program, it can be found in the standard or a user-defined library, or (when appropriate) it is implicitly defined (see 12.1, 12.4 and 12.8). An inline function shall be defined in every translation unit in which it is odr-used.

关于内联函数是否可以在不同的翻译单元中具有不同的实现,它什么也没说.我尝试了以下方法:

It doesn't say anything about whether the inline functions can have different implementations in different translation units. I tried the following:

test-1.cc

#include <iostream> 
inline std::ostream& foo(std::ostream& os)
{
   return os << "Foo_1";
}

void test_1()
{
   foo(std::cout) << std::endl;
}

test-2.cc

#include <iostream> 
inline std::ostream& foo(std::ostream& os)
{
   return os << "Foo_2";
}

void test_2()
{
   foo(std::cout) << std::endl;
}

main.cc

extern void test_1();
extern void test_2();

int main()
{
   test_1();
   test_2();
   return 0;
}

我期望看到以下输出:

Foo_1
Foo_2

相反,我看到了:

Foo_1
Foo_1

我使用g++ 4.7.3对其进行了测试.

I tested it using g++ 4.7.3.

g ++在选择一种内联实现中正确吗?不可能在不同的翻译单元中提供内联函数的不同实现吗?

Is g++ correct in choosing to pick one of the inline implementations? Is it not possible to provide different implementations of inline functions in different translation units?

推荐答案

ISO C ++ 2003§3.2第5段说

ISO C++ 2003 § 3.2 paragraph 5 says

一个类类型(第9节),枚举类型(7.2),内联函数可以有多个定义 具有外部链接(7.1.2),类模板(第14节),非静态功能模板(14.5.5),静态数据 类模板的成员(14.5.1.3),类模板的成员函数(14.5.1.1)或特殊模板- 程序中未指定某些模板参数(14.7、14.5.4)的版本 定义出现在不同的翻译单元中,并且提供的定义满足以下要求- ments.如果给这样一个名为D的实体定义了多个翻译单元,则

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.5), static data member of a class template (14.5.1.3), member function of a class template (14.5.1.1), or template special- ization for which some template parameters are not specified (14.7, 14.5.4) in a program provided that each definition appears in a different translation unit, and provided the definitions satisfy the following require- ments. 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
  • (more requirements follow)

...

如果D的定义不满足这些要求,则行为是不确定的.

If the definitions of D do not satisfy these requirements, then the behavior is undefined.

因此,没有任何非等效的定义是非法的.由于行为是不确定的,因此编译器几乎可以自由执行任何操作,包括选择他喜欢的实现并忽略其他实现.

So, no, non-equivalent definitions are illegal. Since the behaviour is undefined the compiler is pretty much free to do anything, including picking his favorite implementation and ignoring others.

这篇关于内联函数在不同翻译单元中的不同实现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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