aakashbhowmick aakashbhowmick - 3 years ago 66
C++ Question

Why can static member function definitions not have the keyword 'static'?

As per this link on the 'static' keyword in C++ :


The static keyword is only used with the declaration of a static
member, inside the class definition, but not with the definition of
that static member
.


Why is the static keyword prohibited on member function definitions? I do understand that re-declaring a function as 'static' at its definition is redundant. But using it should be harmless during compilation of the function definition as it does not result in any kind of ambiguity. So why do compilers prohibit it?

Answer Source

There's ambiguity alright. The same definition need not be for a member function at all.

Consider this:

namespace foo {
    static void bar();
}

static void foo::bar() {

}

foo::bar is required to be defined with the same linkage specifier.

For member functions, however, static is not a linkage specifier. If it was allowed, the correctness of the definition of foo::bar will be very very context dependent on what foo is. Disallowing static in fact eases the burden on the compiler.

Extending it to members in general, as opposed to just member functions, is a matter of consistency.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download