there is a question about overloading functions. Look at this code:
#include<iostream>
void fv(int){}
void fc(const int){}
void fvr(int&){}
void fcr(const int&){}
void fm(void(*fun)(const int))
{
std::cout << "Constant called" << std::endl;
}
//void fm(void(*fun)(int))
//{
// std::cout << "non Constant called" << std::endl;
//}
void fm(void(*fun)(const int&))
{
std::cout << "Constant ref called" << std::endl;
}
void fm(void(*fun)(int&))
{
std::cout << "non Constant ref called" << std::endl;
}
int main()
{
fm(&fc);
fm(&fv);
fm(&fvr);
fm(&fcr);
return 0;
}
if you uncomment void fm(void(*fun)(int))
function you find that compiler can't statically overload function by pointer on function that accept parameter by value and pointer on function that accept const value. Also, if you uncomment void(*fun)(const int)
and comment void(*fun)(const int)
then all compiles sucessfully. But, if we using references it compiles OK. Don't get why, could you explain me please? Does this mean that pointers to function that accept parameter by value and by const value is same types?
UPD: Top-level const doesn't influence a function signature There is a good explanation why top-level const should be removed.