Why did they insist on using the extern specifier in the examples below?

[basic.link] / 6 (my underline):

The name of the function declared in the block scope and the name of the variable declared in the extext declaration has a link. ...

 static void f(); static int i = 0; void g() { extern void f(); // internal linkage int i; // #2 i has no linkage { extern void f(); // internal linkage <-- extern int i; // #3 external linkage } } 

[basic.link] / 7 :
...

 namespace X { void p() { q(); // error: q not yet declared extern void q(); // q is a member of namespace X <-- } void middle() { q(); // error: q not yet declared } void q() { /* ... */ } // definition of X::q } void q() { /* ... */ } // some other, unrelated q 

The extern pointers indicated by arrows are not needed, given the first sentence in paragraph [basic.link] / 6, in bold above. Or am I missing something?

+7
c ++ language-lawyer linkage c ++ 17
source share
2 answers

extern should emphasize the relevant comments, indicating that extern not valid in certain circumstances (due to the rules set out in this paragraph).

In the first example, f has an internal connection, although extern declared because it was first declared static in the namespace.

In the second, exmaple extern does not affect the declaration, because q also declared in the namespace area without it (and X::q takes precedence over ::q ).

+1
source share

I think the examples lead to some plausible but wrong ideas that might otherwise have arisen.

In paragraph 6, we can expect that f() has an external connection, since it means that extern “usually” (ie in the file area) means, but it is actually an internal connection due to the declaration of static further up.

In paragraph 7, someone might expect extern void q(); will make q() available outside (or in free speech in relation to the external) p() , so it could be called in middle() , but this also will not happen.

Both will still be true without the extern keyword, but then it extern as no surprise that people expecting extern mean something else.

+1
source share

All Articles