Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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() {}
like image 517
igntec Avatar asked Aug 13 '16 12:08

igntec


1 Answers

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.

like image 182
Kerrek SB Avatar answered Sep 23 '22 15:09

Kerrek SB