Why can't a static member function have a cv-qualifier? [duplicate] - c++

This question already has answers here:
C++ - Why static member function can't be created with 'const' qualifier
(5 answers)
C++ : Why cant static functions be declared as const or volatile or const volatile [duplicate]
(4 answers)
Closed 3 months ago.
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;
}

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

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.

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.

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.

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

Related

Declare const static method for singleton to not change member variables [duplicate]

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?
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.
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!
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
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.
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.

What's the difference between const member function and const-ref member function [duplicate]

This question already has an answer here:
c++ - const member func, that can be called upon lvalue instances only, using a ref-qualifier
(1 answer)
Closed 2 years ago.
For the following code:
class C
{
public:
void fun() const {};
void fun() const & {};
};
I know that this is illegal, since we cannot overload with const and const &. However, my question is: we already have const member function in the old standard, why did we need to introduce the const & member function? Are there any semantic differences between a const member function and a const-ref member function?
What's the difference between const member function and const-ref member function
Lvalue ref qualified function cannot be called on rvalue instance arguments (unless the function is also const qualified). Unqualified member functions can be.

Same function with same signatures in the same class/struct? Overload?

I have seen a code portion in the boost example which is used to construct a state machine. What confused me is the two member functions ElapsedTime()? Is this allowed for the two functions to have the same signatures such as function name and parameters type?
I have googled a lot but without any luck to find relevant info about this. Any advice on this would be greatly appreciated.
struct Active : sc::simple_state< Active, StopWatch, Stopped >
{
public:
typedef sc::transition< EvReset, Active > reactions;
Active() : elapsedTime_( 0.0 ) {}
double ElapsedTime() const { return elapsedTime_; }
double & ElapsedTime() { return elapsedTime_; }
private:
double elapsedTime_;
};
Signature of a function is defined by the name and by the argument types.
You have two functions with the same name, but they do not get the same arguments!
You might wonder how could it be?
So, each member function gets another parameter implicitly: this is the "this" pointer. A pointer to the object who called that method.
When you add const in the end of the method, you specify the "this" argument as a const pointer to const. In the other method (without the const), the type of "this" is just const pointer.
Hence, you have two methods with different signature, and there is no problem at all.
They do not have the same signatures - one is const and another one is not. Constness is part of member function signature.
The signatures are different because one has const qualifier.
http://www.cprogramming.com/tutorial/const_correctness.html
Is this allowed
This usage is allowed as over.load#2.2 states:
Member function declarations with the same name and the same parameter-type-list cannot be overloaded if any of them is a static member function declaration ([class.static]). Likewise, member function template declarations with the same name, the same parameter-type-list, and the same template parameter lists cannot be overloaded if any of them is a static member function template declaration. The types of the implicit object parameters constructed for the member functions for the purpose of overload resolution ([over.match.funcs]) are not considered when comparing parameter-type-lists for enforcement of this rule. In contrast, if there is no static member function declaration among a set of member function declarations with the same name and the same parameter-type-list, then these member function declarations can be overloaded if they differ in the type of their implicit object parameter. [ Example: The following illustrates this distinction:
(end quote)
Now, in your example the first overload has a const qualifer which means that its implicit object parameter has type const Active& while the second overload has no const qualifer meaning that its implicit object parameter is of type Active&. Moroever, there is no static member function declaration for ElapsedTime with the same paremeer-type-list. Hence using the above quoted statement, this means that the given usage is allowed.
Note
Note that the currently accepted answer is technically incorrect because it claims that this is a const pointer to const. But in reality the standard specifies that inside a const qualified member function the this pointer is of type const X* while inside a non-const qualified member function the this pointer is of type X*.

the function error in vs2010

I write following in vs2010:
int test() const;
it tell me the const used incorrectly.and said:
Non member function does not allow the use of type qualifier
I wonder why,does it the problem of vs?or how to use such function in vs?
const when applied to a function is only applicable to non-static member functions, not free functions or static member functions.
class A
{
void f1() const; // OK
static void f2() const; // Not OK
};
void f3() const; // Not OK
From the C++ standard:
9.3.1 Nonstatic member functions
A non-static member function may be declared const, volatile, or const volatile.
9.4.1 Static member functions
A static member function shall not be declared const, volatile, or const volatile.
const in that context means this method will not modify any member variables. If it is not a method of a class (i.e. it is a free floating function), then it has no meaning.

What does the `const` keyword do when it is after a function? [duplicate]

This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
What is the meaning of a const at end of a member function?
I have seen some classes that have something like this.
void something() const;
What does const means?
In addition to what SRM said (that the function cannot alter any members of the class (which is not exactly true, e.g. in the case of mutable members, which still can be altered)), it means that you can call this member function on things that are const. So if you get a Foo const& foo as a parameter, you can only call members that are declared const like in your question.
It means that something may not modify member variables of the class.
The exception to the rule is if there are member variables declared with the mutable keyword.
For example, suppose you have a std::map< int, int > var member variable and a method that does the following:
int Class::method () const {
if (var.find (42) != var.end ()) {
return var[42];
}
return 0;
}
That will not compile since var[42] modifies var if 42 is not in the container. Declaring var as mutable allows you to pass compilation.
It means the function will not modify any member variables. To be more precise, it means the function cannot alter any non-static or immutable member variables of that class
To be absolutely precise - it makes the implicit this pointer in the function a pointer to a const object.
From 9.2.1 "The this pointer""
The type of this in a member function of a class X is X*. If the member function is declared const, the type of this is const X*, if the member function is declared volatile, the type of this is volatile X*, and if the member function is declared const volatile, the type of this is const volatile X*.
All the other behaviors (that you can't modify the object members, that you can call the function using a const object, etc) fall from that.
it declares that this function can not change any data. Makes it a Read Only Function.