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
123
votes
11 answers

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

Consider this code: struct A { void foo() const { std::cout << "const" << std::endl; } private: void foo() { std::cout << "non - const" << std::endl; } }; int main() { A a; …
Narek
  • 38,779
  • 79
  • 233
  • 389
113
votes
5 answers

Why would adding a method add an ambiguous call, if it wouldn't be involved in the ambiguity

I have this class public class Overloaded { public void ComplexOverloadResolution(params string[] something) { Console.WriteLine("Normal Winner"); } public void ComplexOverloadResolution(M something) { …
McKay
  • 12,334
  • 7
  • 53
  • 76
62
votes
3 answers

How is ambiguity determined in the overload resolution algorithm?

I'm trying to understand the overloading resolution method. Why is this ambiguous: void func(double, int, int, double) {} void func(int, double, double, double) {} void main() { func(1, 2, 3, 4); } but this isn't? void func(int, int, int,…
Ana M
  • 657
  • 6
  • 9
61
votes
2 answers

What are the pitfalls of ADL?

Some time ago I read an article that explained several pitfalls of argument dependent lookup, but I cannot find it anymore. It was about gaining access to things that you should not have access to or something like that. So I thought I'd ask here:…
fredoverflow
  • 256,549
  • 94
  • 388
  • 662
60
votes
4 answers

Why does the number of elements in a initializer list cause an ambiguous call error?

Why are the first two calls to doSomething OK by the compiler, but using two elements in the list causes an ambiguous call? #include #include void doSomething(const std::vector& data) {} void doSomething(const…
koolbanana
  • 719
  • 5
  • 11
56
votes
1 answer

Why does pointer decay take priority over a deduced template?

Let's say I'm writing a function to print the length of a string: template void foo(const char (&s)[N]) { std::cout << "array, size=" << N-1 << std::endl; } foo("hello") // prints array, size=5 Now I want to extend foo to support…
Barry
  • 286,269
  • 29
  • 621
  • 977
53
votes
4 answers

String literal matches bool overload instead of std::string

I am trying to write a C++ class that has some overloaded methods: class Output { public: static void Print(bool value) { std::cout << value ? "True" : "False"; } static void Print(std::string value) { std::cout…
Matthew Layton
  • 39,871
  • 52
  • 185
  • 313
50
votes
2 answers

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

I wrote those two overloads: int func(int, int) { return 1; } int func(double, double) { return 2; } When I call them with the obvious two calling schemes, i.e. func(1, 1) and func(1.0, 1.0), the first and the second overloaded functions…
Tortellini Teusday
  • 1,335
  • 1
  • 12
  • 21
46
votes
1 answer

Why does the compiler prefer f(const void*) to f(const std::string &)?

Consider the following piece of code: #include #include // void f(const char *) { std::cout << "const char *"; } // <-- comment on purpose void f(const std::string &) { std::cout << "const std::string &"; } void f(const void *)…
46
votes
3 answers

C# Method overload resolution not selecting concrete generic override

This complete C# program illustrates the issue: public abstract class Executor { public abstract void Execute(T item); } class StringExecutor : Executor { public void Execute(object item) { // why does this method…
MarkPflug
  • 28,292
  • 8
  • 46
  • 54
43
votes
5 answers

std::function fails to distinguish overloaded functions

I am trying to understand why std::function is not able to distinguish between overloaded functions. #include void add(int,int){} class A {}; void add (A, A){} int main(){ std::function func = add; } In the…
MS Srikkanth
  • 3,829
  • 3
  • 19
  • 33
42
votes
4 answers

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

#include using namespace std; template void f(T&&) { cout << "f(T&&)" << endl; } template void f(const T&&) { cout << "f(const T&&)" << endl; } struct A {}; const A g1() { return {}; } const int g2() { return…
xmllmx
  • 39,765
  • 26
  • 162
  • 323
40
votes
3 answers

Why is template constructor preferred to copy constructor?

#include struct uct { uct() { std::cerr << "default" << std::endl; } uct(const uct &) { std::cerr << "copy" << std::endl; } uct( uct&&) { std::cerr << "move" << std::endl; } uct(const int &) { std::cerr << "int"…
38
votes
2 answers

How does the method overload resolution system decide which method to call when a null value is passed?

So for instance you have a type like: public class EffectOptions { public EffectOptions ( params object [ ] options ) {} public EffectOptions ( IEnumerable options ) {} public EffectOptions ( string name ) {} public…
Joan Venge
  • 315,713
  • 212
  • 479
  • 689
37
votes
1 answer

Peculiar overload resolution with while (true)

I was implementing sync/async overloads when I came across this peculiar situation: When I have a regular lambda expression without parameters or a return value it goes to the Run overload with the Action parameter, which is predictable. But when…
i3arnon
  • 113,022
  • 33
  • 324
  • 344
1
2 3
48 49