It appears that in C++ extern
(NOT followed by a language-linkage string literal) makes no difference on function declarations at namespace scope (Difference between declaration of function with extern and without it). But does it have any effect whatsoever on block scope function declarations? Or is a local function declaration without extern
always equivalent to one with extern
?
namespace {
void f() {
extern void g(); // has external linkage
g();
}
void f2() {
void g(); // always the same, as if, without extern
g();
}
}
Thanks!
The rules here come from [basic.link]:
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.
So there is no difference between a block scope function declaration with and without external
. But note the interesting example:
static void f(); void g() { extern void f(); // internal linkage }
Here, the block scope f
redeclares ::f
and receives its same linkage: internal. Even though it's marked extern
. But the presence of absence of the extern
keyword is immaterial