Questions tagged [overload-resolution]

Overload resolution is a language mechanism to select among several viable function overloads. Its rules are intricate and often surprising, even for experienced users.

Overload resolution is a language mechanism to select among several viable function overloads.

Possible functions overloads are first determined from a candidate list resolution that itself includes inter ala. name lookups, template or generic determination and instantiation, promotions and conversions.

733 questions
0
votes
1 answer

Can method overloading discriminate automatically between an interface implementations?

I have two classes implementing an interface : public interface Vehicle {…} public class Car implements Vehicle {…} public class Shoes implements Vehicle {…} At the user level I am dealing with the interface, and functions are usually of the form…
0
votes
1 answer

std::string implicit conversion priority, string_view over const char*

I have two overloaded functions: void Set(const char * str) { std::cout << "const char * setter: " << str << "\n"; } void Set(const std::string_view & sv) { std::cout << "string_view setter: " << sv << "\n"; } And I call Set() on a std::string. It…
Anton
  • 1,458
  • 1
  • 14
  • 28
0
votes
1 answer

Google mock overload resolution

I'm using Google Mock to Unit Test. I want the ability to switch between fake and mock. This is the Fake. class Fake { public: void test(char val1, int val2) { } }; And here's the Mock. class Mock { public: MOCK_METHOD2(func, void(int,…
Tinyden
  • 524
  • 4
  • 13
0
votes
1 answer

Swift optional promotion vs generic overload resolution

Please consider the following code: protocol P {} class X {} class Y: P {} func foo(_ closure: (T) -> Void) { print(type(of: closure)) } func foo(_ closure: (T) -> Void) where T: P { print(type(of: closure)) } let xClosure: (X?) -> Void = {…
imre
  • 1,667
  • 1
  • 14
  • 28
0
votes
2 answers

Understanding compiler choice in resolving function overloading

I am compiling on a platform where int= 32 bits (gcc arm none eabi, cortex M3, GCC version 9) with the dialect set to C++17. I have a overloaded a method with both template versions and plain versions of the same method. The normal overloads are…
Andrew Goedhart
  • 937
  • 9
  • 17
0
votes
2 answers

Why Overload resolution is unable to decide which version of an overloaded function to initialize an std::function object?

I have this example about std::function: int add(int x, int y, int z) {return x + y + z;} int add(int a, int b) {return a + b;} int main() { std::function fn = add; // error int(*pfn)(int, int) = add; // OK fn = pfn; //…
Maestro
  • 2,512
  • 9
  • 24
0
votes
2 answers

Confused about C#'s extension method overload resolution

Consider the following code: using System; using System.Linq; using System.Collections.Generic; public static class Ex { public static IEnumerable Take(this IEnumerable source, long cnt) { return source; } } public…
Blindy
  • 65,249
  • 10
  • 91
  • 131
0
votes
2 answers

Ranking of implicit conversion sequences [f(int) and f(const int&)]

Consider the following functions: void f(int) {...} void f(const int&) {...} They are different and their definitions compile together successfully. But is there a way to call any of them when they both participate in overload resolution? And if…
Lassie
  • 853
  • 8
  • 18
0
votes
2 answers

What's the rank of implicitly conversion for copy-list-initialization

#include struct A{ A(int){ } }; struct B{ B() = default; B(A){ } B(B const&){} B(B&&){} }; int main(){ B b({0}); } For the given codes, the candidate functions are: #1 B::B(A) #2 B::B(const B&) #3 …
xmh0511
  • 7,010
  • 1
  • 9
  • 36
0
votes
1 answer

Expression.Equal solution that honors "Equals" overloads and implicit operators

When compiling code at runtime using expression trees, we may find ourselves needing to check objects of indeterminate types for equality. If we were to simply code this up by hand for each case, the compiler would take into account many things for…
Timo
  • 7,992
  • 4
  • 49
  • 67
0
votes
2 answers

How can I specialize an algorithm for iterators that point to complex values?

I am trying to write an algorithm that works on iterators (similar to the STL algorithms) however I need to write a specialization of the algorithm to act differently when the iterators point to complex values vs regular double values. Here is a…
0
votes
1 answer

How do I resolve an overloaded function by it's signature as a template parameter?

I'm writing a delegate library and stubled upon this problem: Let's say I have overloaded functions named foo like this: int foo(double d); double foo(int d); How would I write my template argument list if I want to resolve which function is meant…
0
votes
1 answer

Kotlin : Overload resolution ambiguity

New to Kotlin. I am using Apache Camel and have created a route using the process transformer like so: from("snmp:blahblah...") .routeId("CamelSnmpRoute") ... .process { <<< Here logger.debug("Log…
MeanwhileInHell
  • 6,780
  • 17
  • 57
  • 106
0
votes
1 answer

Overload resolution for pointer to derived class

I'd accept a pointer to a base class and then call different functions based on its derived type. [ EDIT Problem is: accept is a public method of a Manager class, which handles and stores lots of A, B1, B2 (shared_ptr of) instances. Manager will…
Patrizio Bertoni
  • 2,582
  • 31
  • 43
0
votes
0 answers

If you have two global functions, for a `Widget` and the other for a `double`, and the `Widget` class defines a cast-to-double, what will happen?

My question is if conversion functions (user-defined casts) take precedence over direct calls. Suppose that you overload a function: void display_widget( double f ); void display_widget( Widget a ); My question is, which has higher…