C++ - Why static member function can't be created with 'const' qualifier
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.
Why can't a static member function have a cv-qualifier?
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*
.
C++ : Why cant static functions be declared as const or volatile or const volatile
Because that's what the standard says:
9.4.1 Static member functions [class.static.mfct]
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 declaredconst
,
volatile
, orconst volatile
. (emphasis mine)
The reason for this is that a const
(or volatile
or virtual
) static
method wouldn't make sense (in the traditional sense, see below). For example, const
implies you can't modify the object's members, but in the case of statics, there's no object to talk about.
You could argue that a const
static
could apply to other static
members, but this option was regarded as pointless.
static const function and variable
This code is illegal, it's const qualified static method
class Sample{
public:
static int fun() const {
return 0;
}
};
This code is OK, it's a const qualified non-static method
class Sample{
public:
int fun() const {
return 0;
}
};
This code is OK, it's a static method with a const return type
class Sample{
public:
static const int fun() {
return 0;
}
};
I guess the problem is that you don't realise what it means that a method is const
. It has nothing to do with the return type, it's about the type of the this
pointer within the method (as the quote says).
Why there is no concept of const-correctness for class's static member functions?
cv-qualifiers
affect the function's signature. So you could have:
class A {
static int s_common;
public:
static void getCommon () const { };
static void getCommon () { };
};
Now... how would you call the const
one? There's no const
object to call it on (well, you could call it on a const
object, but that's not the point).
I'm just guessing here, there probably are other reasons. :)
Why can a const member function modify a static data member?
It's the rule, that's all. And for good reason.
The const
qualifier on a member function means that you cannot modify non-mutable
non-static
class member variables.
By way of offering some rationalisation, the this
pointer in a const
qualified member function is a const
type, and this
is inherently related to an instance of a class. static
members are not related to a class instance. You don't need an instance to modify a static
member: you can do it, in your case, by writing A::a = 10;
.
So, in your first case, think of a = 10;
as shorthand for A::a = 10;
and in the second case, think of it as shorthand for this->a = 10;
, which is not compilable since the type of this
is const A*
.
What is throwing the error the static or the const?
A static
method cannot be marked as const
: since it doesn't work on an instance, it makes no sense to specify that it cannot modify it.
(you could argue that for static
methods it could have referred to static
methods that cannot modify the static
data associated with the class; however, this would have no use anyway, since you cannot have a const
class or form a const
pointer or reference to a class, as in C++ classes aren't objects)
Why I can't use const qualfier in my function?
This function should be declared without the trailiing const
. It is a free function and doesn't belong to a class, so it is not meaningful for the function to be const
.
bool isDon(string const& name) {
return name == "Don";
}
Note that you can also use a lambda of the form
ptr = find_if(names, names+5, [](string const& name){ return name == "Don"; });
Why modifiers like const not allowed on nonmember functions
const
modifier states that a member function won't modify data members of the object the function belongs to.
It's like an assurance that calling that function on an object aobj
won't modify the internal state of that object. So, assuming that aobj
is declared const
too, you will still be able to invoke that function on it; on the contrary, you would not be able to invoke non const
function members.
If a function is not member of a class, it makes no sense to apply const
modifier. On another language, it could have meant that the function wasn't able to modify global variables, maybe; but that language is not C++.
Related Topics
Is There Support in C++/Stl for Sorting Objects by Attribute
How to Copy/Paste from the Clipboard in C++
Overloading Assignment Operator in C++
Will Consteval Functions Allow Template Parameters Dependent on Function Arguments
Defining a Variable in the Condition Part of an If-Statement
Is There Any Way a C/C++ Program Can Crash Before Main()
Boost Zip_Iterator and Std::Sort
In C++ What Causes an Assignment to Evaluate as True or False When Used in a Control Structure
For Purposes of Ordering, Is Atomic Read-Modify-Write One Operation or Two
How to Avoid Static Member Function When Using Gsl with C++
Must New Always Be Followed by Delete
What Is a Reference-To-Pointer
Why Is #Define Bad and What Is the Proper Substitute
How to Do the Equivalent of Memset(This, ...) Without Clobbering the Vtbl
Xcode with Boost:Linker(Id) Warning About Visibility Settings
How to Automatically Avoiding Stepping into Certain Functions in Visual Studio