I have a question regarding function matching. consider this code:
main.hpp:
struct Foo
{
Foo(double = 1.0)
{
std::cout << "Foo constructor\n";
}
operator double()
{
std::cout << "Foo double\n";
return double(1.0);
}
operator int()
{
std::cout << "Foo int\n";
return int(1);
}
};
void Bar(Foo &f)
{
std::cout << "Bar Foo\n";
}
void Bar(int)
{
std::cout << "Bar int\n";
}
main.cpp:
double d;
Bar(Foo(d));
output:
Foo constructor
Foo int
Bar int
however if i change void Bar(Foo &f) to void Bar(const Foo &f), output changes to
Foo constructor
Bar Foo
I'm not sure why const causes to call Bar(Foo) instead of Bar(int) even though Foo(d) is not const.