views:

74

answers:

2

Section 7.1 of the C++ Standard mentions about 'extern' as a storage class specifier.

N3126 - "The extern specifier can be applied only to the names of variables and functions. The extern specifier cannot be used in the declaration of class members or function parameters. For the linkage of a name declared with an extern specifier, see 3.5. [ Note: The extern keyword can also be used in explicit-instantiations and linkage-specifications, but it is not a storage-class-specifier in such contexts. —end note ]

I understand about this keyword and it's use in the context of 'linkage specification', but I am unable to get a grasp on the use of 'extern' as a storage specifier.

  1. Don't all 'extern' names have static storage duration?
  2. If Answer to 1 is yes, then why this redundancy? C Compatibility?
+1  A: 

It's not really a storage specifier per se. It comes before the variable name much as other storage specifiers do, but all it does is shut the compiler up and tell the linker that it has more work to do.

Ignacio Vazquez-Abrams
Actually, looking again at e.g. 'mutable', I fail to understand what that has to do with 'storage class'. What exactly does 'storage class' mean/do?
Chubsdad
Some storage classes (`static`, `auto`, `register`) specify what memory the variable will be stored in. Other storage classes (`const`, `volatile`) affect how the compiler generates code to access them. `extern` does not really do either.
Ignacio Vazquez-Abrams
are const and volatile storage classes? C++ treats them as type specifiers rather than as storage class specifiers. Not sure about C though.
Chubsdad
@Chubsad: No, `const` and `volatile` are not storage class specifiers, they are type qualifiers. As you are only allowed one storage class specifier in a declaration, if `const` where one then (e.g.) `static const int x = 5;` would be illegal. This is true in both C and C++ as far as I'm aware.
Charles Bailey
+1  A: 

extern is a storage class specifier. This is just a fact of the language grammar. extern has a number of effects on the semantics of a program depending on where it is used. It doesn't have the single same effect everywhere. It influences the storage duration and linkage of objects and it also helps determine whether some declarations are also definitions or not.

E.g.:

int a; // Ex1

extern int b; // Ex2

For example, if Ex1 and Ex2 where at global scope then they would both refer to objects with static storage duration and external linkage. In C++, though, the first would be a definition (tentative definition in C) and the second would not. In this example extern has not changed the storage duration or linkage of the declared object.

If Ex1 and Ex2 occurred in a function body then a would refer to an object with automatic storage duration and no linkage but b would refer to an object with external linkage and static storage duration. In this example, extern has affected the meaning of the declaration in both linkage, storage duration and whether or not it is a definition.

Finally, in C++, here is an example where the only effect of extern is changing the linkage from internal to external.

const int c = 5; // static storage duration, internal linkage

extern const int d = 10; // static storage duration, external linkage
Charles Bailey