C++ - Why Static Member Function Can't Be Created with 'Const' Qualifier

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

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



Leave a reply



Submit