Why can't a static member function have a cv-qualifier?
Solution 1
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 bevirtual
. There shall not be astatic
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
, orconst 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 thethis
pointer. For a const-qualified member function of classC
, thethis
pointer is of typeC const*
, whereas for a member function that is not const-qualified, thethis
pointer is of typeC*
.
Solution 2
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.
Solution 3
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.
Solution 4
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.
Solution 5
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
Sahil Sareen
Software Engineer II Foundation Member and Game Developer GNOME https://blogs.gnome.org/ssareen/ B.Tech., CSc NIT Durgapur, India [email protected]
Updated on November 07, 2020Comments
-
Sahil Sareen over 3 years
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; }
-
Martin York over 10 yearsQuoting 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.
-
James Kanze over 10 years@LokiAstari That would change the meaning of
const
when applied to a function. The meaning of making a functionconst
is to change the type ofthis
fromT*
toT 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 whatconst
on a function means. -
juanchopanza over 10 years@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. -
Martin York over 10 years@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.
-
TT_ stands with Russia about 10 years+1 Also, what a great name - al-Khwārizmī , I did not know before how it should be written in English.