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

Trying to understand C++11 §3.3.1/4

51 views
Skip to first unread message

Wake up Brazil

unread,
Sep 29, 2014, 10:52:07 AM9/29/14
to
Consider this snippet:

#include <iostream>
extern int A;
static int A = 101;
class A{};
int main()
{
std::cout << A << '\n';
}

From §3.5/4 we have that the first declaration `extern int A;` gives the name `A` external linkage and the second declaration `static int A = 101;` gives the name `A` internal linkage. From §3.3.1/4 we can say this is an error, because we have two different entities (external and internal linkages) with the same name in the same declarative region.

All three compilers (g++, clang and VS2013) produce an error in this cases.

Consider now this example:

#include <iostream>
static int A = 101;
extern int A;
class A{};
int main()
{
std::cout << A << '\n';
}

Everything that I said before applies here, but g++ and clang compile and execute this code. I believe these compilers are not compliant with §3.3.1/4.

Does anyone have any other idea about this?

Victor Bazarov

unread,
Sep 29, 2014, 11:23:25 AM9/29/14
to
If the makers of g++ (and clang) misread the Standard a bit, they
interpreted that any declaration, even with 'extern' that follows a
declaration with 'static', does not override the linkage of the object,
but declares the same object, which has the same linkage as has been
declared already, i.e. internal here. See [basic.link]/6. However,
that particular part of the Standard talks specifically about functions
and variables declared in *block* scope, as the example that follows shows.

I cannot imagine any other error by the developers of those compilers.
And I agree with your determination that the same object cannot have two
linkages when declared *in the same scope*.

Curiously, I seem to have never seen any comments by compiler
implementors here. Either they are too busy to participate in any
community activities, or there are so few of them that their posts are
simply lost in the torrent of other articles, or they purposely avoid
the forum... It would be interesting to hear/see an interpretation of
theirs, however.

V
--
I do not respond to top-posted replies, please don't ask

Wake up Brazil

unread,
Sep 29, 2014, 1:09:49 PM9/29/14
to
Thanks for your valuable reply, and I agree with you that [basic.link]/6 doesn't apply here.
0 new messages