38

This is the error:

error: static member function ‘static void myClass::myfunct()’ cannot have cv-qualifier

Can someone please explain this error and why const cannot be used.

#include<iostream>
class myClass{      
   static void myfunct() const 
   { 
     //do something
   }
};

int main()
{
   //some code
   return 0;
}
Raymond Chen
  • 44,448
  • 11
  • 96
  • 135
Sahil Sareen
  • 1,813
  • 3
  • 25
  • 40
  • What are you trying to do with the word `const`? – Raymond Chen Nov 06 '13 at 13:08
  • 2
    const on static function could be used to prevent the static function from changing the state of the class. Then why the error or so the standard? – Sahil Sareen Nov 06 '13 at 14:23
  • You seem to be changing your question. First you ask "What does it mean?" Now you're asking "Why doesn't it mean what I want it to mean?" Questions about why a language is designed a certain way don't really fit this site since they aren't practical. (Whether the language is designed the way you like doesn't alter the fact that you have to program to the language you have, not the language you wish you had.) At any rate, there is some discussion of the rationale in the linked duplicate. – Raymond Chen Nov 06 '13 at 14:28

5 Answers5

53

Worth quoting the standard here

9.4.1 Static member functions

2) [ Note: A static member function does not have a this pointer (9.3.2). —end note ] A static member function shall not be virtual. There shall not be a static and a non-static member function with the same name and the same parameter types (13.1).

A static member function shall not be declared const, volatile, or const volatile.

static functions have no this parameter. They need no cv-qualifiers.

See this answer by James McNellis

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*.

Community
  • 1
  • 1
Sadique
  • 22,572
  • 7
  • 65
  • 91
  • 5
    Quoting the standard is the only correct response. Other answers have suggested "does not make sense" as a reason. You could (I am not going to) argue that const on static member would prevent the static function from mutating the state of the class (i.e. static member variables) and thus could make sense its just the standards committee made a choice that was not how it was going to work. – Martin York Nov 06 '13 at 13:14
  • @LokiAstari That would change the meaning of `const` when applied to a function. The meaning of making a function `const` is to change the type of `this` from `T*` to `T const*`. This meaning can clearly only apply to non-static members, which is why the committee chose what it did. To have things otherwise, you'ld have to change the definition of what `const` on a function means. – James Kanze Nov 06 '13 at 13:54
  • 3
    @LokiAstari the standard doesn't usually tell you *why* something is. Quoting the standard often only tells you what you already know. In this case, you can infer a bit more because there is a single mention of the `this` pointer, but this is far from explaining anything. – juanchopanza Nov 06 '13 at 16:55
  • 1
    @JamesKanze: I understand the reason they did it and why. But when they built the standard they could have quite easily argued the other way and applied a special rule for class members. Personally I am glad they did not (the simple and clean is best) but it basically boils down to an arbitrator choice (with arguments available for both sides). So your only choice is to quote the standard. – Martin York Nov 06 '13 at 18:12
  • 1
    +1 Also, what a great name - al-Khwārizmī , I did not know before how it should be written in English. – TT_ stands with Russia Apr 02 '14 at 23:43
13

A static member function is not bound to an instance of its class, so it does not make sense for it to be const and/or volatile (i.e. "cv-qualified"), because there is no instance to which const or volatile can be applied to in calling that function.

juanchopanza
  • 223,364
  • 34
  • 402
  • 480
4

It doesn't make sense to write const there, because the function is static and therefore there is no class instance on which to imbue a const context. Thus it is treated as an error.

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
1

Qualifier const in a member function declaration is applied to the pointer to the object of class this. As static functions are not bound to objects of the class they have no implicit parameter this. So the qualifier const has no any sense for these functions.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
1

Const qualifier for member functions means that the function will not change the object instance and can be called on const objects. Static member functions are not bound to any object instance and therefore it makes no sense for them to be const, since you don't call the static member function on any object. That is why the standard forbids it.

class Foo
{
public:
    void memberFunc();
    static void staticMemberFunc();
}

Foo f;
f.memberFunc();          // called on an object instance
Foo::staticMemberFunc(); // not called on an object instance
rozina
  • 4,120
  • 27
  • 49