C++ Quiz91 of 173
Difficulty:
⬅️ Prev: 197Quiz ID: 198Next: 205 ➡️

According to the C++23 standard, what is the output of this program?

#include <iostream>

namespace A {
  extern "C" { int x; }
};

namespace B {
  extern "C" { int x; }
};

int A::x = 0;

int main() {
  std::cout << B::x;
  A::x=1;
  std::cout << B::x;
}
Hint

x does not have implicit extern specification.

Reveal answer

The program has a compilation error

Explanation

A::x and B::x actually refer to the same variable, and int x; is a definition, not a declaration. Defining the same variable twice results in a compilation error.

Why do A::x and B::x refer to the same variable?

§[dcl.link]¶7:

Two declarations declare the same entity if they (re)introduce the same name, one declares a function or variable with C language linkage, and the other declares such an entity or declares a variable that belongs to the global scope.

Both A::x and B::x introduce the name x with C language linkage, so they declare the same entity.

Now, why is int x; a definition, and not an extern declaration? The standard declares that "a declaration directly contained in a linkage-specification is treated as if it contains the extern specifier". x is not directly contained in the linkage specification, and thus doesn't have the implicit extern. Therefore it's a definition and repeated definition of x causes a compilation error.

§[dcl.link]¶8 has the explanation and a relevant example:

A declaration directly contained in a linkage-specification is treated as if it contains the extern specifier (§[dcl.stc]) for the purpose of determining the linkage of the declared name and whether it is a definition. (...)

[Example 5:

(...)
extern "C" int i;                   // declaration
extern "C" {
  int i;                            // definition
}
(...)

— end example]

Note: This question is identical to #191 except for the {} surrounding int x;. How does this affect the outcome?

⬅️ Prev: 197Quiz ID: 198Next: 205 ➡️