22

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?

5
  • 5
    static on an out-of-class function definition means internal linkage, however class member functions have external linkage. It'd be confusing
    – M.M
    Jul 24, 2017 at 10:23
  • Compiler is always in a state to figure out if a function is a member-function or a global-function. Shouldn't be too confusing for it. :-/
    – johngreen
    Jul 24, 2017 at 10:26
  • @M.M but he's talking about class member functions. Jul 24, 2017 at 10:26
  • @MichaelWalz no kidding
    – M.M
    Jul 24, 2017 at 10:44
  • 3
    @aakashbhowmick it's confusing for humans reading the code, not for the compiler.
    – M.M
    Jul 24, 2017 at 10:44

4 Answers 4

20

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.

5
  • Delphi referred to such functions as "class" functions rather than "static", using the class keyword to indicate them. With hindsight, that may have been a better idea for C++, rather than re-using "static" for a totally different thing... Jul 24, 2017 at 13:15
  • @MarkKCowan - "Overloading" a keyword (and one inherited from C, no less) wasn't the best idea, I agree. I'm not sure using class would have been better (partially because structs and classes are interchangeable, what then? A "struct" member function?). Perhaps an entirely different keyword would have been better. Then compilers could have been made less context dependent then they are today. Jul 24, 2017 at 13:24
  • class is already overloaded for template arguments, where "struct" can't be used in its place even though we can template structs Jul 24, 2017 at 15:09
  • I have accepted this answer as it provides a good reason why compilers would want to enforce this rule.
    – johngreen
    Jul 24, 2017 at 15:24
  • @MarkKCowan - But it's not a case where the mismatch is glaring. If you define a type with the struct keyword, it would be quite weird to designate the function with "class". Well, IMO anyway. Jul 25, 2017 at 5:36
5

The point is, that static has several, very different meanings:

class Foo {
    static void bar();
}

Here the static keyword means that the function bar is associated with the class Foo, but it is not called on an instance of Foo. This meaning of static is strongly connected to object orientation. However, the declaration

static void bar();

means something very different: It means that bar is only visible in file scope, the function cannot be called directly from other compilation units.

You see, if you say static in the class declaration, it does not make any sense to later restrict the function to file scope. And if you have a static function (with file scope), it does not make sense to publish it as part of a class definition in a public header file. The two meanings are so different, that they practically exclude each other.


static has even more, distinct meanings:

void bar() {
    static int hiddenGlobal = 42;
}

is another meaning, that is similar, but not identical to

class Foo {
    static int classGlobal = 6*7;
}

When programming, words don't always the same meaning in all contexts.

4
  • The question was not about the different meanings of 'static' in C++. As a decent C++ programmer, I am aware of all of these.
    – johngreen
    Jul 24, 2017 at 15:21
  • @aakashbhowmick But you asked why your compiler won't allow you to combine two of these different meanings, seemingly thinking, the static would have the same effect in both cases. When you use static on a function outside of class scope, it is entirely different from when you use it within class scope. Jul 24, 2017 at 19:34
  • I'm quite clear about how a static member-function is different from a static global-function. My question was why should a static member-function definition NOT contain the keyword 'static', though the declaration of the same function can ( and should).
    – johngreen
    Jul 25, 2017 at 4:46
  • @AAkashbhowmick Well, syntactically, there would be no difference between a static member-function definition and a file scope function definition. The only difference between the two definitions is, that the member function definition generally needs to use the scope resolution operator. However, that operator may also appear on a file scope function if it's part of a namespace, so it's no syntactic difference, only a semantic one. Jul 25, 2017 at 6:55
1

You have to understand the difference between declaration and implementation, and that will answer your question:

Declaration: Is how C++ functions and methods are seen before compiling the program. It's put in a header file (.h file).

Implementation: Is how the compiler links a declaration to a real task in binary code. The implementation can be compiled on the fly (from source files, .cpp or .cxx or .cc), or can be already compiled (from shared libraries or object files).

Now going back to your question, when you declare something as static, it's something not related to the implementation, but related to how the compiler sees the decleration while compiling the code. For example, if you label functions in source files "static", then that's meaningless, because that information cannot be carried to compiled objects and shared libraries. Why allow it? On the contrary, it could only cause ambiguity.

For the exact same reason, default parameters must go into the header, not the source files. Because source files (that contain implementations), cannot carry the default parameter information to a compiled object.

0

Wild guess but if the definition has static it could be interpreted as a file-scope variable in the C sense.

0

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.