igntec igntec - 3 months ago 13
C++ Question

Extern keyword on definition of static data members and member functions, C++

Does C++ standard allow

extern
keyword on definition of static data members and member functions (provided that linkage matches) ? For example:

struct A
{
static int a; // external linkage
void f(); // external linkage
};

extern int A::a;
extern void A::f() {}

Answer

The extern keyword is not allowed as a storage class specifier on class members. From [dcl.stc]/5:

[...] The extern specifier cannot be used in the declaration of class members or function parameters. [...]

Moreover, definitions are declarations, cf. [basic.def]/2:

A declaration is a definition unless [rules].

Therefore, the extern keyword is not allowed as a storage class specifier on any form of class member declaration, whether on the first declaration that's part of the class definition or on subsequent declarations that are part of out-of-line member definitions.