Skip to content

[SUGGESTION] Force static functions class members to use static class data members #1349

Open
@fro0m

Description

@fro0m

The problem:
Software developers oftentimes use static class member functions to make the function available in the desired namespace but not actually using a static class member inside this function. This is a misuse of static member functions which cause confusion among software developers who see that function is static but it does not use static class members. Static functions must say definitely that it uses static data by their nature, otherwise it mislead software developers who will read this code. Some would say that this is not critical, but this cause the programming language to be less specific and more confusing at the same time.

The solution proposed:
Consider using static class member functions which do not use static class member an error (ill-formed). Restrict from using static class member functions when they do not actually use a static class members. Give a compile error when software developers try to do such a thing. Limitation: The only exception for such style is using a singleton pattern, so static member function static Class& Class::instance() which instantiates static Class instance; or static Class* instance variable.

Effects of the solution:
Improving language simplicity. Make the language easier to learn. Make software developers to clearly understand that static class member functions always use static data members.

update 1:
Considering comments from @gregmarr and @ljleb .
Using a static class member function just to show that this function relates to a namespace is a problem of classic C++ which I described above. Class != namespace from the language perspective. So 1 thing in a language must mean 1 thing and not other things. If a user wants to use static class member function it would mean that this function uses a class static member. Only 1 meaning, no misunderstanding. If a user wants to use a function under the namespace he moves this function in this namespace. Only 1 meaning, no misunderstanding. This simplifies language.
Review the meaning of static keyword below and you will see that static class member functions are super cohesive with static class member data, so it is kind of defect of classic C++ to let static class member functions to use data which has no relation to static data members.

  1. Static Variables

    Function Scope: When declared within a function, a static variable retains its value between function calls. It is initialized only once, and its lifetime extends throughout the program's execution, unlike automatic variables that are reinitialized with each call1
    4
    .
    Global Scope: Declaring a global variable or function as static limits its visibility to the file in which it is declared, preventing access from other files4
    5
    .

  2. Static Class Members

    Class Scope: A static member of a class is shared among all instances of that class. It exists independently of any class instances and can be accessed using the class name rather than an object instance. This includes both static member variables and static member functions1
    2
    6
    .

  3. Static Storage Duration

    The keyword indicates that a variable has static storage duration, meaning it is allocated for the entire duration of the program. This applies to both static local variables and static class members2
    5
    .

  4. Static Functions

    Functions declared as static within a class cannot access non-static members directly and are meant to operate independently of object instances. They can be called without creating an instance of the class1
    2
    .

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions