Why is a public const member function not called when the non-const one is private?

When you call a.foo();, the compiler goes through overload resolution to find the best function to use. When it builds the overload set it finds void foo() const and void foo() Now, since a is not const, the non-const version is the best match, so the compiler picks void foo(). Then the access restrictions are … Read more

Ambiguous call to overloaded function taking int or float when passing a decimal number

Look at the error message from gcc: a.cpp:16: error: call of overloaded ‘function(double, double)’ is ambiguous a.cpp:3: note: candidates are: void function(int, int) a.cpp:9: note: void function(float, float) A call to either function would require truncation, which is why neither is preferred over the other. I suspect you really want void function(double y,double w). Remember … Read more

Why is template constructor preferred to copy constructor?

As far as I know non-template function is always preferred to template function during overload resolution. This is true, only when the specialization and the non template are exactly the same. This is not the case here though. When you call uct u3(u1) The overload sets gets uct(const uct &) uct(uct &) // from the … Read more

Why do primitive and user-defined types act differently when returned as ‘const’ from a function?

I don’t have a quote from the standard, but cppreference confirms my suspicions: A non-class non-array prvalue cannot be cv-qualified. (Note: a function call or cast expression may result in a prvalue of non-class cv-qualified type, but the cv-qualifier is immediately stripped out.) The returned const int is just a normal int prvalue, and makes … Read more

C# Method overload resolution not selecting concrete generic override

Looks like this is mentioned in the C# specification 5.0, 7.5.3 Overload Resolution: Overload resolution selects the function member to invoke in the following distinct contexts within C#: Invocation of a method named in an invocation-expression (§7.6.5.1). Invocation of an instance constructor named in an object-creation-expression (§7.6.10.1). Invocation of an indexer accessor through an element-access … Read more

Why is an overloaded function with two arguments of type double called when passing a long long?

Which function from an overload set is chosen to be called (i.e. overload resolution) depends (in part) on how many arguments of the function call must go through an implicit conversion, and what kind of conversion is needed. The rules that are relevant to your example are: For each pair of viable function F1 and … Read more

String literal matches bool overload instead of std::string

“Hello World” is a string literal of type “array of 12 const char” which can be converted to a “pointer to const char” which can in turn be converted to a bool. That’s precisely what is happening. The compiler prefers this to using std::string‘s conversion constructor. A conversion sequence involving a conversion constructor is known … Read more

Why does pointer decay take priority over a deduced template?

The fundamental reason for this (standard-conforming) ambiguity appears to lie within the cost of conversion: Overload resolution tries to minimize the operations performed to convert an argument to the corresponding parameter. An array is effectively the pointer to its first element though, decorated with some compile-time type information. An array-to-pointer conversion doesn’t cost more than … Read more