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

Can the main function be overloaded?

§3.6.1/2 (C++03) says An implementation shall not predefine the main function. This function shall not be overloaded. It shall have a return type of type int, but otherwise its type is implementation-defined. All implementations shall allow both of the following definitions of main: int main() { /* … */ } int main(int argc, char* argv[]) … Read more

Should you prefer overloading over specialization of function templates?

Short story: overload when you can, specialise when you need to. Long story: C++ treats specialisation and overloads very differently. This is best explained with an example. template <typename T> void foo(T); template <typename T> void foo(T*); // overload of foo(T) template <> void foo<int>(int*); // specialisation of foo(T*) foo(new int); // calls foo<int>(int*); Now … Read more

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

Why can’t an interface implementation return a more specific type?

Usually, I’d say that it would be a case of balancing the benefit against the added complexity of supporting such a feature. (All features take effort to design, document, implement, test, and then developers need to be educated about them too.) Note that there could be some significant complexities if you wanted to support returning … Read more

Java overloading and inheritance rules

The behavior of these method calls is dictated and described by the Java Language Specification (reference section 8.4.9). When a method is invoked (§15.12), the number of actual arguments (and any explicit type arguments) and the compile-time types of the arguments are used, at compile time, to determine the signature of the method that will … Read more

Why does autoboxing make some calls ambiguous in Java?

When you cast the first argument to Object yourself, the compiler will match the method without using autoboxing (JLS3 15.12.2): The first phase (§15.12.2.2) performs overload resolution without permitting boxing or unboxing conversion, or the use of variable arity method invocation. If no applicable method is found during this phase then processing continues to the … Read more

C++ bool returns 0 1 instead of true false

You can use std::boolalpha: Sets the boolalpha format flag for the str stream. When the boolalpha format flag is set, bool values are inserted/extracted as their names: true and false instead of integral values. This flag can be unset with the noboolalpha manipulator. The boolalpha flag is not set in standard streams on initialization. std::cout.setf(std::ios::boolalpha); … Read more

Override and overload in C++

Overloading generally means that you have two or more functions in the same scope having the same name. The function that better matches the arguments when a call is made wins and is called. Important to note, as opposed to calling a virtual function, is that the function that’s called is selected at compile time. … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)