I am trying to understand overloading resolution in C++ through the books listed here. One such example that i wrote to clear my concepts whose output i am unable to understand is given below.
#include <iostream>
struct Name 
{
  operator int() 
  {
      std::cout<<"Name's int version called"<<std::endl;
      return 4;
  }
  operator float() 
  {
      std::cout<<"Name's float version called"<<std::endl;
      return 1.1f;
  }
  
};
int main()
{
    
    double a = Name(); //this works and calls Name's float version. But WHY ISN'T THIS AMBIGIOUS?
    long double b = Name();  //this does not work. WHY IS THIS AMBIGIOUS?
    bool c = Name();  //this does not work. WHY IS THIS AMBIGIOUS? 
     
    return 0;
}
As you can see here the program works when creating double a. But when i try to create objects b and c it gives error.
My questions are:
- Why don't we get the ambiguity error for object - a. That is, among the two conversion operators in class- Name, why the- floatversion is chosen over the- intversion.
- Why/how do we get the ambiguity error for object - bwhich is a- long double. That is just like for- a, i suspected that the- floatversion should have been called but instead we get the error. How is this different from the- double acase above.
- Why/how do we get the ambiguity error for object - cwhich is- bool. In this case, I suspected that the- intversion could have been chosen but instead we get an error. How is this different than the- double aversion which works and uses- floatversion of conversion function.
I just want to understand why/how the first version works but the other two don't.
 
     
    