C++ - Why static member function can't be created with 'const' qualifier

C++StaticLanguage LawyerConst Method

C++ Problem Overview


Today I got a problem. I am in the need of a static member function, const is not a must but a better. But, I didn't succeed in my efforts. Can anybody say why or how?

C++ Solutions


Solution 1 - C++

When you apply the const qualifier to a nonstatic member function, it affects the this pointer. For a const-qualified member function of class C, the this pointer is of type C const*, whereas for a member function that is not const-qualified, the this pointer is of type C*.

A static member function does not have a this pointer (such a function is not called on a particular instance of a class), so const qualification of a static member function doesn't make any sense.

Solution 2 - C++

I agree with your question, but unfortunately the C++ is designed that way. For example:

class A {
  int i;         //<--- accessed with 'this'
  static int s;  //<---- accessed without 'this'
public:
  static void foo ()  const // <-- imaginary const
  {}
};

As of today, the const is considered in context of this. In a way, it's narrow. It can be made broader by applying this const beyond this pointer.
i.e. the "proposed" const, which may also apply to static functions, will restrict the static members from any modification.

In the example code, if foo() can be made const, then in that function, A::s cannot be modified. I can't see any language side effects, if this rule is added to standard. On the contrary, it's amusing that why such rule doesn't exist!

Solution 3 - C++

It is unfortunate that C++ doesn't accept it as per design but logically there are few use cases in which it validates well.

A function which is class level valid(static) might not change any static data, may be it will just query data should be const. May be it should be like

if(Object)
    MakeThisConstant()
else
    MakeStaticDataConstant() // Only in the scope but static data cannot be constant so may be it should in some scenarios

Solution 4 - C++

Without getting into the details, it's because there may or may not be an object modified by the function, so const is ambiguous to the compiler.

Recall that const keeps objects constant, but there may or may not be an object here to keep constant.

Solution 5 - C++

A 'const member function' is not allowed to modify the object it is called on, but static member functions are not called on any object. It is used directly by scope resolution operator. Thus having a const static member function makes no sense, hence it is illegal.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionprabhakaranView Question on Stackoverflow
Solution 1 - C++James McNellisView Answer on Stackoverflow
Solution 2 - C++iammilindView Answer on Stackoverflow
Solution 3 - C++Raghavendar ReddyView Answer on Stackoverflow
Solution 4 - C++Don LarynxView Answer on Stackoverflow
Solution 5 - C++nihal dixitView Answer on Stackoverflow