gpt4 book ai didi

c++ - 链接内联虚方法

转载 作者:可可西里 更新时间:2023-11-01 16:30:11 25 4
gpt4 key购买 nike

GCC 4.7.2 编译这段代码如下:

  • Obj::defaultNumber() 中对Obj::getNumber() 的引用是内联的
  • Obj::getNumber() 的主体是单独编译和导出的,可以从不同的翻译单元链接到。

VC++2012 在链接步骤失败:

  • 错误 1 ​​错误 LNK2001:main.obj 中未解析的外部符号“public:virtual int __thiscall Obj::getNumber(unsigned int)”

VC++ 似乎在 Obj::defaultNumber() 中内联调用,但没有在符号表中导出 getNumber()

VC++可以通过以下方式之一进行编译:

  • getNumber() 定义中删除 inline 关键字,或者
  • getNumber() 声明中删除 virtual 关键字(为什么!?)

乍一看,GCC 的行为似乎更有帮助/直观。也许熟悉标准的人可以在这里为我指明正确的方向。

  • 在这种情况下,这两个编译器是否具有一致的行为?
  • 如果方法是非虚拟的,为什么 VC++ 可以工作?

.

// Obj.h

class Obj
{
public:
virtual int getNumber(unsigned int i);
virtual int defaultNumber();
};

// Obj.cpp

static const int int_table[2] = { -1, 1 };

inline int Obj::getNumber(unsigned int i)
{
return int_table[i];
}

int Obj::defaultNumber()
{
return getNumber(0);
}

// main.cpp

#include <iostream>
#include "Obj.h"

int main()
{
Obj obj;
std::cout << "getNumber(1): " << obj.getNumber(1) << std::endl;
std::cout << "defaultNumber(): " << obj.defaultNumber() << std::endl;
}

最佳答案

我把答案完全改了一次,为什么不改两次呢?对于任何拼写错误,已经很晚了。

在这种情况下,这两个编译器是否具有一致的行为?

他们两个都有。这是未定义的行为。

如果方法是非虚拟的,为什么 VC++ 可以工作?

因为您的代码不符合标准,编译器可以自行决定解释您的代码。

为什么编译器是一致的?

该标准规定,如果虚拟成员不是纯成员,则按照单一定义规则使用,并且每个翻译单元都需要自己定义的 odr-used 内联函数。

§ 3.2

2) "[...] A virtual member function is odr-used if it is not pure. [...]"
3) "[...] An inline function shall be defined in every translation unit in which it is odr-used. [...]"

§ 7.1.2

4) An inline function shall be defined in every translation unit in which it is odr-used and shall have exactly the same definition in every case (3.2). [...]

此外,该标准要求函数在每个翻译单元内内联声明,但允许编译器忽略任何诊断。

§ 7.1.2

4) [...] If a function with external linkage is declared inline in one translation unit, it shall be declared inline in all translation units in which it appears; no diagnostic is required. [...]

由于您的 getNumber() 未在 main.cppObj.cpp 中声明为内联,因此您处于这里有未定义的行为。

(edit) 我对此(请参阅下面的标准引用)的解释是,编译器不需要检查该函数是否在其出现的任何地方都被声明为内联。我不知道,但怀疑这个“编译器不必检查”-规则不是指“内联函数应在每个使用它的翻译单元中定义的句子[...] ".

即使在 main 中额外定义了 getNumber(从 main 的角度来看从未被声明为内联),MSVC 也会编译,即使 Obj.cpp 实现仍然被声明为内联并存在于符号表。即使代码不符合标准,编译器的这种行为也是允许的:未定义行为 (UB)。(/edit)

因此,两个编译器都可以自由地接受并编译或拒绝您的代码。

如果方法是非虚拟的,为什么 VC++ 可以工作?

这里的问题是:为什么 VC++ 不实例化 virtual 内联函数,而是在 virtual 被遗漏的情况下实例化?

您可能会在 SO 周围阅读这些声明,其中说“UB 可能会炸毁您的房子,杀死您的妈妈,世界末日”等。我不知道为什么如果函数是虚拟的则没有函数实例,否则会有。我想这就是 UB 的意义所在 - 奇怪的事情。

为什么 GCC 和 VS (without virtual) 都提供内联函数的外部链接函数实例?

编译器不需要实际替换内联函数,它们很可能会生成函数的实例。由于默认情况下内联函数具有外部链接,因此如果创建了实例,则可能从 main 调用 getNumber

  • 编译器只会将一个未定义的外部添加到 main.obj 中(没问题,这里不是内联的)。
  • 链接器会在 Obj.obj 中找到完全相同的外部符号并愉快地链接。

7.1.2

2) An implementation is not required to perform this inline substitution at the point of call; however, even if this inline substitution is omitted, the other rules for inline functions defined by 7.1.2 shall still be respected.

4) An inline function with external linkage shall have the same address in all translation units.

§ 9.3

3) Member functions of a class in namespace scope have external linkage.

MSDN inline, __inline, __forceinline

The inline keyword tells the compiler that inline expansion is preferred. However, the compiler can create a separate instance of the function (instantiate) and create standard calling linkages instead of inserting the code inline. Two cases where this can happen are:

  • Recursive functions.
  • Functions that are referred to through a pointer elsewhere in the translation unit.

These reasons may interfere with inlining, as may others, at the discretion of the compiler; you should not depend on the inline specifier to cause a function to be inlined.

所以可能有一个“其他”原因使编译器实例化它并创建一个由 main.obj 使用的符号但我不知道为什么如果函数是 virtual.

结论:

编译器可能会也可能不会实例化内联函数,它可能会或可能不会接受同一函数在一个翻译单元中内联而在另一个翻译单元中非内联。要定义您的行为,您需要

  • 将函数的每个声明都内联或全部非内联
  • 为每个翻译单元提供一个单独的内联函数定义(即在头文件中的内联定义或在源文件中的单独定义)

关于c++ - 链接内联虚方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17536196/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com