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
20
votes
1 answer

Overload resolution and partial template ordering

Consider this simple pair of function templates. template void foo(T& ) { std::cout << __PRETTY_FUNCTION__ << '\n'; } template void foo(const C& ) { std::cout << __PRETTY_FUNCTION__ << '\n'; } If we call foo with a…
Barry
  • 286,269
  • 29
  • 621
  • 977
19
votes
3 answers

Should this compile? Overload resolution and implicit conversions

This example seems to compile with VC10 and gcc (though my version of gcc is very old). EDIT: R. Martinho Fernandez tried this on gcc 4.7 and the behaviour is still the same. struct Base { operator double() const { return 0.0; } }; struct…
ryaner
  • 3,927
  • 4
  • 20
  • 23
19
votes
2 answers

Method overload resolution with regards to generics and IEnumerable

I noticed this the other day, say you have two overloaded methods: public void Print(IEnumerable items) { Console.WriteLine("IEnumerable T"); } public void Print(T item) { Console.WriteLine("Single T"); } This code: public void…
theburningmonk
  • 15,701
  • 14
  • 61
  • 104
19
votes
3 answers

Why is the Short method calling the integer method?

public class Yikes { public static void go(Long n) { System.out.print("Long "); } public static void go(Short n) { System.out.print("Short "); } public static void go(int n) { System.out.print("int "); …
PSR
  • 39,804
  • 41
  • 111
  • 151
18
votes
2 answers

Why isn't this call to a template ambiguous?

I declare two templates, the first converts the argument x from type T to type U and the second from type U to type T. If I call cast with 10, the compiler does not complain. I think both meet the requirements to be used and therefore there should…
18
votes
1 answer

Overload resolution with an empty brace initializer: pointer or reference?

I ran into a real-life WTF moment when I discovered that the code below outputs "pointer". #include #include template struct bla { static void f(const T*) { std::cout << "pointer\n"; } static void f(const…
rubenvb
  • 74,642
  • 33
  • 187
  • 332
18
votes
1 answer

How to inspect the overload resolution set for a given call site

How can I inspect the overload resolution set? I have 4 competing functions used in multiple call sites. In one call site, I'm expecting one function to be called, but another one is picked up by the compiler. I don't know why/it's not trivial. To…
gnzlbg
  • 7,135
  • 5
  • 53
  • 106
17
votes
2 answers

Different versions of g++ have inconsistent result of overload resolution

When I used g++ 5.4.0, the sample code below worked as expected, but after I updated the g++ to 10.2.0, the result was changed. I also tested the sample code on clang++ 11.0.1, and the result was the same as g++ 5.4.0. I have searched some relevant…
sizzle
  • 173
  • 5
17
votes
3 answers

Function overload for string literals lvalue and rvalue reference

The function test below is overloaded for lvalue empty strings, lvalue non-empty strings and rvalue strings. I tried to compile with Clang and GCC but in both case I do not have the result I expected. #include void test(const char…
Baptistou
  • 1,749
  • 1
  • 13
  • 24
17
votes
4 answers

How does "std::cout << std::endl;" compile?

Most IO stream manipulators are regular functions with the following signature: std::ios_base& func( std::ios_base& str ); However some manipulators (including the most frequently used ones - std::endl and std::flush) are templates of the following…
Leon
  • 31,443
  • 4
  • 72
  • 97
17
votes
1 answer

Overload resolution difference between gcc and clang involving move constructor and 'Derived(Base&&)' constructor

GCC (tested with 4.9) accepts the following testcase: struct Base {}; struct Derived : Base { Derived(); explicit Derived(const Derived&); explicit Derived(Derived&&); explicit Derived(const Base&); Derived(Base&&); }; Derived…
HighCommander4
  • 50,428
  • 24
  • 122
  • 194
17
votes
2 answers

C++ template functions overload resolution

I have the following code: #include template void f (T) { std::cout << "f(T)" << std::endl; } template void f (bool) { std::cout << "f(bool)" << std::endl; } int main ( ) { f(true); // #1 prints…
lisyarus
  • 15,025
  • 3
  • 43
  • 68
17
votes
1 answer

Does an lvalue argument prefer an lvalue reference parameter over a universal reference?

While playing with universal references, I came across this instance where clang and gcc disagree on overload resolution. #include struct foo {}; template void bar(T&) { std::cout << "void bar(T&)\n"; } template
17
votes
2 answers

F# and interface-implemented members

I have a vexing error. type Animal = abstract member Name : string type Dog (name : string) = interface Animal with member this.Name : string = name let pluto = new Dog("Pluto") let name = pluto.Name The last line,…
16
votes
1 answer

Overloaded function templates that differ only in their return types in C++

It is well known that ordinary functions that differ only in their return type cannot be overloaded in C++. But this limitation does not hold for overloaded function templates, for example: int f(auto) { return 1; } auto f(auto) { return 2; } All…
Fedor
  • 17,146
  • 13
  • 40
  • 131