Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Trying to understand §3.3.1/4 [duplicate]

Apparently from §3.3.1/4, this snippet doesn't compile because it contains two different entities with the same name A in the global namespace, extern int A; and static int A = 101;. That is, one has external and the other has internal linkage.

live example

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

Why then, does this code compile?

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

Edit

I think the accepted answer for the question, of which this one is considered a dup, basically says that in the second snippet, variable A still has internal linkage, despite the extern declaration. But this is in disagreement with paragraph §3.5/4 that I mentioned below in a comment to @dyp.

§3.5/4:

An unnamed namespace or a namespace declared directly or indirectly within an unnamed namespace has internal linkage. All other namespaces have external linkage. A name having namespace scope that has not been given internal linkage above has the same linkage as the enclosing namespace if it is the name of

— a variable; or

...

Edit 1:

The OP uses §3.5/6 to justify his answer to the other question.

§3.5/6 (emphasis mine):

The name of a function declared in block scope and the name of a variable declared by a block scope extern declaration have linkage. If there is a visible declaration of an entity with linkage having the same name and type, ignoring entities declared outside the innermost enclosing namespace scope, the block scope declaration declares that same entity and receives the linkage of the previous declaration. If there is more than one such matching entity, the program is ill-formed. Otherwise, if no matching entity is found, the block scope entity receives external linkage.

It's clear this answer doesn't apply to the snippets shown on my question, as the declarations of the variable A are not block scope declarations.

Edit 2:

This issue with "ready" status says that §7.1.1/7 should be deleted because it's false.

like image 716
Wake up Brazil Avatar asked Sep 28 '14 13:09

Wake up Brazil


1 Answers

The extern specifier does not require that the name has external linkage.

The first example

extern int A; is a declaration of a name A with external linkage - but the external linkage is implied since it's a declaration at namespace scope (outside of an unnamed namespace).

static int A; declares a name with internal linkage.

The two declarations disagree about the linkage, hence the error.

The second example

Here, we first declare static int A;, i.e. a name A with internal linkage.

The declaration extern int A; doesn't declare A with external linkage, it merely redeclares a name that is found via name lookup.

[dcl.stc]/7

The linkages implied by successive declarations for a given entity shall agree. That is, within a given scope, each declaration declaring the same variable name or the same overloading of a function name shall imply the same linkage. Each function in a given set of overloaded functions can have a different linkage, however.

[ Example:

static char* f(); // f() has internal linkage
char* f() // f() still has internal linkage
{ /* ... */ }

char* g(); // g() has external linkage
static char* g() // error: inconsistent linkage
{ /* ... */ }

// [left out some examples with `inline`]

static void n();
inline void n(); // internal linkage

static int a; // a has internal linkage
int a; // error: two definitions

static int b; // b has internal linkage
extern int b; // b still has internal linkage

int c; // c has external linkage
static int c; // error: inconsistent linkage

extern int d; // d has external linkage
static int d; // error: inconsistent linkage

-- end example ]

like image 167
dyp Avatar answered Oct 10 '22 00:10

dyp