cs-
cs-

Reputation: 485

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).

(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. X.h looks like this:

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

)

Question: Is it correct or not, for the compiler to emit a symbol for 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.

Upvotes: 0

Views: 117

Answers (1)

Brian Bi
Brian Bi

Reputation: 119307

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. 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).

Upvotes: 1

Related Questions