I have a few questions about communication from the following variables. On examples 7.1.1 / 7 from C ++ 03 and experimenting with compilers (Comeau, Clang and GCC), I came to the following types of links:
First static , then extern
static int a;
It is clear to me that, in accordance with section 3.5: (a) implies an internal connection. And (b) also implies an internal relationship, because the name "a" is declared static (by (a)).
extern first, then static
extern int b;
First, (c) implies an external connection. But (d) implies an internal relationship, because the name "b" is declared static by (d). This is incorrect in accordance with 7.1.1 / 7, since the implied connection is incompatible.
const first, then extern
const double pi1 = 3.14; // (e) extern const double pi1; // (f) valid and 'pi1' is internal
First, (e) implies an internal relationship, since it is a constant and is not declared explicitly extern or previously implied by an external relationship. And (f) should imply external communication and be a mistake, as it explicitly declares the name extern, but compilers keep it internal! Why is that? What is my question.
extern first, then const
extern const double pi2;
Now (g) implies an external connection, because we explicitly declare extern. And (h) also implies an external relationship because (g) is explicitly declared extern.
I experimentally discovered a relationship for 3 and 4 with the following pattern (the second argument is needed for external binding)
template<typename T, T&> struct ensure { }; ensure<const double, pi1> e1;
Summary: The discussion with Charles Bailey turned out to be very fruitful and showed that two interpretations of 3.5/3 possible, where an important marker point reads
A name that has a namespace scope (3.3.5) has an internal relationship if that name
- An object or reference that is explicitly declared as const, and neither explicitly declared extern, nor previously declared by external relations;
If we look at point (f) , then the two interpretations will come to different conclusions, as shown below
The first interpretation notes that pi1 declared const , but also extern declared. Thus, the variable has an external relationship.
The second interpretation interprets both occurrences of the “declared” to mean the same declaration. So this means that it is declared const , but not extern const . Note that (e) declared const , not extern const , so we give pi1 internal connection.
Now, which interpretation is correct? I cannot determine from this statement, but the compilers seem to interpret this second way. In particular, if we take the first interpretation, the last cited part 3.5/3 will be redundant, because there would be no valid scenario in which the name would be declared const and previously declared with external communication, but without an explicit extern .
c ++ static extern linkage
Johannes Schaub - litb
source share