Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Linkage confusion with "const" variabes

1 view
Skip to first unread message

Johannes Schaub (litb)

unread,
Aug 21, 2010, 10:14:06 PM8/21/10
to
I have a few questions about the linkage from the following variables. By
examples of 7.1.1/7 of C++03 and experimenting with compilers, I came to the
following linkage kinds:

#1
static int a; #a
extern int a; #b // valid, 'a' still internal

#2
extern int b; #c
static int b; #d // invalid!

#3
const double pi1 = 3.14; #e
extern const double pi1; #f // valid and 'pi1' is internal

#4
extern const double pi2; #g
const double pi2 = 3.14; #h // valid but 'pi2' is now external


In #1, it's clear to me with accordance to 3.5: #a implies internal linkage.
And #b also implies internal linkage, because the name "a" is declared
static (by #a).

In #2, #c implies external linkage. But #d implies internal linkage because
the name "b" is declared static by #d. Thus -> invalid.

In #3, #e implies internal linkage, because it is const, and neither
declared explicit extern nor previously implied external linkage. And #f
should imply extern linkage and be an error, because it explicitly declares
the name extern, but the compilers keep it internal! Why so?.

In #4, #g implies external linkage because we explicitly declared extern.
And #h also implies external linkage because #g explicitly declared extern.

Thanks for all your insight!


--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Johannes Schaub (litb)

unread,
Aug 22, 2010, 9:02:49 PM8/22/10
to
Johannes Schaub (litb) wrote:

For all that are interested, Charles Bailey and me had a discussion about
the semantics of #3 on Stackoverflow, and it turned out that there are
different ways to interpret section 3.5. See this link:
http://stackoverflow.com/questions/3538807/linkage-of-various-const-static-variables .

0 new messages