whether it is possible to overload function by const specifier. That is, we have two functions, one constant the other is not, can we say that the constant function overloaded non-const function ?
Yes and No.
It depends on where you put the const specifier.
When defining member functions, this is possible (Yes-part):
int f() { /*code*/ } //invoke this function on non-const object
int f() const { /*code*/ } //ok : invoke this function on const object
Note that in the absence of the first function, even non-const object will invoke the second function (i.e const member function) and in the absence of the second function, you wouldn't be able to invoke the first function on const objects!
But this is not possible (No-part):
int g() { /*code*/ }
const int g() { /*code*/ } //error: redefinition
irrespective of whether they're member functions or free functions.
Per ยง 13.1 / 2:
It's not possible to put const in return-type to overload:
Function declarations that differ only in the return type cannot be
overloaded.
int func();
const int func(); // Error
It's not possible to put const in parameter-list to overload:
Parameter declarations that differ only in the presence or absence of
const and/or volatile are equivalent.
void func(int x);
void func(const int x); // Error
BUT, it's possible:
const and volatile type-specifiers buried within a parameter type
specification are significant and can be used to distinguish
overloaded function declarations.
void func(int &x);
void func(const int &x); // OK
And, it's possible to put const at end of method declaration to distinguish overloads:
int func();
int func() const; // OK
Related
When I was writing an overide function with const parameter instead of non-const parameter, I thought the compiler would report an error, because the base function has non-const parameter, but it succeeded to compile it. Why?
My code:
#include <iostream>
class A
{
public:
virtual uint64_t cal(uint64_t value)
{
std::cout << value << std::endl;
return value;
}
};
class B : public A
{
public:
uint64_t cal(const uint64_t value) override;
};
uint64_t B::cal(const uint64_t value)
{
std::cout << value + 1 << std::endl;
return (value+1);
}
int main()
{
B b;
b.cal(1);
return 0;
}
Why?
Because the top const qualifier of a function argument is ignored in a declaration.
uint64_t cal(uint64_t value);
and
uint64_t cal(const uint64_t value);
declare the exact same type of function. cal is a function taking a uint64_t and returning a uint64_t. The const qualifier makes no difference for calling code, since value is always a copy of the passed in argument.
The only difference is in the function body, where the const qualifier will prevent you from modifying the paraemter. But that is an implementation detail.
This difference can even raise coding style questions. For instance see
Is it better to remove "const" in front of "primitive" types used as function parameters in the header?
Note that top-level cv-qualifiers are dropped when considering function type, so uint64_t cal(uint64_t) and uint64_t cal(const uint64_t) are considered as the same function type.
(emphasis mine)
The type of each function parameter in the parameter list is determined according to the following rules:
4) Top-level cv-qualifiers are dropped from the parameter type (This adjustment only affects the function type, but doesn't modify the property of the parameter: int f(const int p, decltype(p)*); and int f(int, const int*); declare the same function)
Top level const of parameters is not a part of the signature of the function. The only requirement for overloading is that the signatures must match.
#include <type_traits>
int main()
{
static_assert(std::is_same_v<int(int), int(const int)>);
}
Compiles fine
Given the code below:
class A
{
public:
A(): value( 0 ) {}
int* get()
{
return &value;
}
const int& get() const
{
return value;
}
private:
int value;
};
int main()
{
A a;
const int& ref_value = a.get();
}
results in the following compilation error:
prog.cpp: In function 'int main()':
prog.cpp:23:35: error: invalid conversion from 'int*' to 'int'
const int& ref_value = a.get();
^
It seems that the overloaded get() method with const modifier does get ignored completely and the compiler sees only the non-const definition of it. It is somehow understandable since the a object is not constant. One solution would be to make the a object constant. Though there are other two solutions that makes the code compileable:
Change the signature of the const get() method by different name or other parameters added.
int* get();
const int& get_changed() const; <-- this gets called
Change the non-const get() method to return a reference instead pointer.
int& get(); <-- this gets called
const int& get() const;
though with
int* get();
const int& get() const;
we have a compiler error.
What puzzles me is the reason behind all of these behavior.
When you have both a const and non-const overload of the same function with the same parameters, which one gets called depends only on the constness of the object on which you're invoking the function. So invoking on a non-const a must call the non-const overload.
It's exactly the same situation as this:
void foo(int *p);
void foo(const int *p);
int main()
{
int i;
const int ci;
foo(&i); // Calls the first overload
foo(&ci); // Calls the second overload
}
A const-qualified function can be called on a non-const-qualified object, but that requires a "nonconst to const" conversion. If there's an overload which doesn't require such a conversion (is a better match), it will be preferred.
The reason is that the compiler can't do overload resolution based on a different return type.
Only the function signature is taken into account for overload resolution. And the function signature consists only of the function name, the parameters, and the cv qualifiers - not the return type.
I am wandering why C++ chooses to call non const methods on non const object for overloaded methods differing only by const method signature, Namely:
#include<iostream>
class Foo
{
public:
Foo() {}
int bar() const { std::cout<<"const version called"<<std::endl; }
int bar() { std::cout<<"version called"<<std::endl; }
};
int main()
{
Foo f;
f.bar();
const Foo g;
g.bar();
return 0;
}
I understand that for g object, being const, the const version of bar is called. But how about f? The output is
version called
const version called
Thanks for your insights.
This is how overload resolution works.
First, a list of candidate functions is constructed. In your case it consists of two functions:
int Foo::bar() const;
int Foo::bar();
Each function in the list is converted to accept this argument roughly like this:
int Foo_bar(const Foo* this);
int Foo_bar(Foo* this);
Then the choice is made according to the "Best viable function" (13.3.3) part of the C++ standard, which says, in short, that the best function is the one requiring the least number of conversions for its arguments. So if you have Foo f; and call f.bar(), the best function is the one accepting non-const this, because it requires no conversions, whereas the const version would require a qualification conversion for this from Foo* to const Foo*.
A non-const object can call both const as well as non-const versions of particular function. Whereas const object can only call const version of that function.
How can one invoke the second (overloaded) function?
(This example is present in Savitch's C++ textbook.)
(1) int& f(); // will be used in any l-value invocation
(2) const int& f() const; // will be used in any r-value invocation
I thought the first one is invoked in (a) and the second one in (b). But it is not.
(a) f() = 123; // the first one is invoked.
(b) f() + 3; // the first one is also invoked.
Only member functions can be const. So let's assume f actually is a member function in the textbook.
When the compiler has a choice between a const and a non const member function it will only use the const one if it has to. This is when the object the function is called on is const.
class A {
public:
int &f();
const int& f() const;
};
void func()
{
A a;
a.f(); // calls non const version
const A ca;
ca.f(); // call const version
}
(1) int& f(); // will be used in any l-value invocation
(2) const int& f() const; // will be used in any r-value invocation
These comments are wrong. If they appear in a textbook then I would recommend getting a different textbook.
Version 2 will be used when the expression denoting the object has const type, otherwise version 1 is used. It is nothing at all to do with lvalues and rvalues. Using Eelke's class definition:
A().f(); // invokes (1) on an rvalue
A const a;
a.f(); // invokes (2) on an lvalue
In your examples you don't actually show whether you are working on a const instance or not, but judging by your results, both must have been on a non-const one.
Overloading works on function parameters not by return type.
double f();
int f();
These are not overloading.
But these are:
double f();
int f(int);
This question already has answers here:
Meaning of 'const' last in a function declaration of a class?
(12 answers)
Closed 7 years ago.
In C++ sometimes I see declarations like below:
return_type function_name( datatype parameter1, datatype parameter2 ) const
{ /*................*/}
What does this const type qualifier exact do in this case?
The const qualifier at the end of a member function declaration indicates that the function can be called on objects which are themselves const. const member functions promise not to change the state of any non-mutable data members.
const member functions can also, of course, be called on non-const objects (and still make the same promise).
Member functions can be overloaded on const-ness as well. For example:
class A {
public:
A(int val) : mValue(val) {}
int value() const { return mValue; }
void value(int newVal) { mValue = newVal; }
private:
int mValue;
};
A obj1(1);
const A obj2(2);
obj1.value(3); // okay
obj2.value(3); // Forbidden--can't call non-const function on const object
obj1.value(obj2.value()); // Calls non-const on obj1 after calling const on obj2
$9.3.1/3 states-
"A nonstatic member function may be declared const, volatile, or const volatile. These cvqualifiers affect the type of the this pointer (9.3.2). They also affect the function type (8.3.5) of the member function; a member function declared const is a const member function, a member function declared volatile is a volatile member function and a member function declared const volatile is a const volatile member function."
So here is the summary:
a) A const qualifier can be used only for class non static member functions
b) cv qualification for function participate in overloading
struct X{
int x;
void f() const{
cout << typeid(this).name();
// this->x = 2; // error
}
void f(){
cout << typeid(this).name();
this->x = 2; // ok
}
};
int main(){
X x;
x.f(); // Calls non const version as const qualification is required
// to match parameter to argument for the const version
X const xc;
xc.f(); // Calls const version as this is an exact match (identity
// conversion)
}
It means that it doesn't modify the object, so you can call that method with a const object.
i.e.
class MyClass {
public:
int ConvertToInteger() const;
};
Means that if you have
const MyClass myClass;
you can call
int cValue = myClass.ConvertToInteger();
without a compile error, because the method declaration indicates it doesn't change the object's data.