简体   繁体   English

class 带外连杆 有底座带内连杆

[英]class with external linkage has base with internal linkage

#include "X.h"
namespace {
  class B { /* ... */ };
}

class D : public B {
  X myx;
  D();
};

D::D() { /* ... */ }

D (with external linkage) derives from B (with internal linkage). D (具有外部链接)源自B (具有内部链接)。

(Real scenario: B is the base of all test classes, and D is a test for X. The leaf classes are friends of the classes they are designed to test, to allow testing their private members. Xh looks like this: (真实场景:B 是所有测试类的基础,D 是对 X 的测试。叶子类是它们要测试的类的朋友,以允许测试它们的私有成员。Xh 看起来像这样:

class D;
class X { friend class D; /* ... */ };

) )

Question: Is it correct or not, for the compiler to emit a symbol for D::D()?问题:编译器为 D::D() 发出符号是否正确? The behavior we are used to is that functions are emitted unless they do not have external linkage.我们习惯的行为是发出函数,除非它们没有外部链接。 On the other hand, D cannot be instantiated in another translation unit, because other translation units cannot have the same B.另一方面,D 不能在另一个翻译单元中实例化,因为其他翻译单元不能具有相同的 B。

The question you're asking is about the implementation details of a toolchain.您要问的问题是关于工具链的实现细节。 The Standard does not prescribe that compilers "emit symbols", but merely that the implementation has some means of resolving external entity references during the final phase of translation.该标准没有规定编译器“发出符号”,而只是规定实现在翻译的最后阶段有一些解决外部实体引用的方法。 Since it's not possible for any other translation unit to define the same entity D , the compiler does not need to place any information into the translated translation unit that would make it possible to locate D::D .由于任何其他翻译单元都不可能定义相同的实体D ,因此编译器不需要将任何信息放入已翻译的翻译单元中,从而可以找到D::D On the other hand, if the compiler does choose to emit a symbol, and another translation unit also defines D::D() and those two symbols are merged by the linker, the program may behave strangely, but this is acceptable since the program is ill-formed NDR (meaning that the Standard does not impose any requirements upon the implementation with respect to that program).另一方面,如果编译器确实选择发出一个符号,并且另一个翻译单元也定义了D::D()并且这两个符号被 linker 合并,则程序可能会出现奇怪的行为,但这是可以接受的,因为程序是格式错误的 NDR(意味着该标准没有对该程序的实施提出任何要求)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM