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;
}namespace is a C++ concept not present in C. How does this affect extern "C" variables declared inside namespaces?
01
Due to the extern "C" specifications, A::x and B::x actually refer to the same variable.
x is first initialized to 0, then main() starts, 0 is printed, x is incremented to 1, and finally 1 is printed.
§[dcl.link]¶7 in the C++ standard:
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.