This is an unofficial snapshot of the ISO/IEC JTC1 SC22 WG21 Core Issues List revision 114a. See http://www.open-std.org/jtc1/sc22/wg21/ for the official list.

2024-04-18


2212. Typedef changing linkage after use

Section: 9.2.4  [dcl.typedef]     Status: CD5     Submitter: Richard Smith     Date: 2015-12-09

[Accepted at the July, 2019 meeting as part of paper P1766R1 (Mitigating minor modules maladies)].

Consider:

  typedef struct { void f() { extern decltype(*this) x; void *p = &x; } } X;

This forms a complete anonymous type, builds a variable of that type, which is declared, but not defined, then the variable is odr-used, then the class is given a typedef name for linkage purposes, which changes the linkage of the class and the variable.

More insidious examples can be constructed where the use of the class's linkage happens while parsing the body of the class, not one of its methods, so we can't entirely fix this by delaying the delayed parts of the class a bit longer.

It might be reasonable to expect an implementation to look ahead past the close brace for a typedef name for linkage when it sees typedef struct {, but the possibility that the typedef keyword might be after the close brace means even that would not be entirely correct.

One approach would be to make this ill-formed by fixating the linkage of the type at the point where it is used in a way that requires linkage, and giving an error when the linkage would change. Another approach would be to limit an anonymous type to the feature subset of C (no this, no member functions, no static members).