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.
Related
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 is the reason why pointers to member functions, can't point to const member functions?
struct A {
void g() {};
void f() const {}
};
Later in code:
void (A::* fun)() = &A::f;
This code produces:
error: cannot convert ‘void (A::*)()const’ to ‘void (A::*)()’ in initialization
Of course it compiles with &A::g instead of &A::f.
In opposite situation:
void (A::* fun)() const = &A::g;
The error is:
error: cannot convert ‘void (A::*)()’ to ‘void (A::*)()const’ in initialization
The second case is rather clear. const pointer isn't expected to modify the object so it can't hold the function which does it. But why it's not possible to assign const member function to non-const member function as in the first case?
It looks like the rule for normal pointers where casting const to non-const would allow to modify the value, but I don't see the point here, where const-correctness is checked in function definition, before such assignment.
What is the reason why pointers to member functions, can't point to const member functions?
Because the const modifier is part of the function signature. Once you declare a function pointer, that function pointer can only be used to assign pointers to function that have the same function signature.
Non-static member functions have an extra hidden this parameter. Given the existence of that that extra hidden parameter, a non-static void A::f() const; behaves much like void A__f(const A *__this), and the behaviour you see for member functions models the behaviour for non-member functions.
void f(void *);
void (*pf)(const void *) = f; // also an error
As for whether it could break on any implementation, I suppose in theory, an implementation is permitted to read a void * parameter from a different register than a const void * parameter, and if so, the result of the conversion (were it valid) could not be used to call f properly. I have no idea why any implementor would make such a decision, and I don't know of any real implementation that does so, but it's one allowed by the standard.
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
While writing the following function abs, I get the error:
non-member function unsigned int abs(const T&) cannot have cv-qualifier.
template<typename T>
inline unsigned int abs(const T& t) const
{
return t>0?t:-t;
}
After removing the const qualifier for the function there is no error. Since I am not modifying t inside the function the above code should have compiled. I am wondering why I got the error?
Your desire not to modify t is expressed in const T& t. The ending const specifies that you will not modify any member variable of the class abs belongs to.
Since there is no class where this function belongs to, you get an error.
The const modifier at the end of the function declaration applies to the hidden this parameter for member functions.
As this is a free function, there is no this and that modifier is not needed.
The t parameter already has its own const in the parameter list.
The cv-qualifier on a member function specifies that the this pointer is to have indirected type const (or volatile, const volatile) and that therefore the member function can be called on instances with that qualification.
Free functions (and class static functions) don't have a this pointer.
As we all know, const keyword followed after the argument list indicates that this is a pointer to a pointer constant.
There is a non-member function, it does not belong to the class, so add const opposite end error occurs.
Solution to the problem: is to either become a class member function or remove the const keyword const opposite end
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.