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
25
votes
2 answers

Template partial ordering - why does partial deduction succeed here

Consider the following simple (to the extent that template questions ever are) example: #include template struct identity; template <> struct identity { using type = int; }; template void bar(T, T ) {…
Barry
  • 286,269
  • 29
  • 621
  • 977
25
votes
2 answers

Why does a value of an enum with a fixed underlying type of char resolve to fct(int) instead of fct(char)?

This problem came up when answering this question about overload resolution with enums. While the case for long long was definitely a bug in MSVC2012NovCTP (according to the standard text and a test with gcc 4.7.1), I cannot figure out why the…
GManNickG
  • 494,350
  • 52
  • 494
  • 543
24
votes
1 answer

If the address of a function can not be resolved during deduction, is it SFINAE or a compiler error?

In C++0x SFINAE rules have been simplified such that any invalid expression or type that occurs in the "immediate context" of deduction does not result in a compiler error but rather in deduction failure (SFINAE). My question is this: If I take…
Faisal Vali
  • 32,723
  • 8
  • 42
  • 45
23
votes
5 answers

Overload resolution and virtual methods

Consider the following code (it's a little long, but hopefully you can follow): class A { } class B : A { } class C { public virtual void Foo(B b) { Console.WriteLine("base.Foo(B)"); } } class D: C { public override void…
Dean Harding
  • 71,468
  • 13
  • 145
  • 180
22
votes
1 answer

How does template argument deduction work when an overloaded function is involved as an argument?

This is the more sophisticated question mentioned in How does overload resolution work when an argument is an overloaded function? Below code compiles without any problem: void foo() {} void foo(int) {} void foo(double) {} void foo(int, double)…
Leon
  • 31,443
  • 4
  • 72
  • 97
22
votes
3 answers

C++ template functions priority

#include template void foo(U&, T&) { std::cout << "first"; } template void foo(int&, const T&) { std::cout << "second"; } int main() { int a; double g = 2.; foo(a, g); // prints…
Ashot
  • 10,807
  • 14
  • 66
  • 117
22
votes
1 answer

How can I programmatically do method overload resolution in C#?

When the C# compiler interprets a method invocation it must use (static) argument types to determine which overload is actually being invoked. I want to be able to do this programmatically. If I have the name of a method (a string), the type that…
22
votes
3 answers

Overload resolution, templates and inheritance

#include struct A {}; struct B : public A {}; template void foo(const T &x) { std::cout << "Called template" << std::endl; } void foo(const A &a) { std::cout << "Called A" << std::endl; } int main() { foo(A()); …
Emil Eriksson
  • 2,110
  • 1
  • 21
  • 31
22
votes
1 answer

Ambiguity involving templated conversion operator and implicit copy constructor

clang and gcc differ in behaviour for the following code: struct foo { foo(int); }; struct waldo { template operator T(); }; int main() { waldo w; foo f{w}; } This code is accepted by clang, with the foo(int)…
HighCommander4
  • 50,428
  • 24
  • 122
  • 194
21
votes
1 answer

Why cannot C# resolve the correct overload in this case?

I've come across a strange situation which is non-ambiguous, yet the overload resolver doesn't think so. Consider: public static class Program { delegate int IntDel(); delegate string StringDel(); delegate void ParamIntDel(int x); …
Vilx-
  • 104,512
  • 87
  • 279
  • 422
21
votes
4 answers

In c++, why does the compiler choose the non-const function when the const would work also?

For example, suppose I have a class: class Foo { public: std::string& Name() { m_maybe_modified = true; return m_name; } const std::string& Name() const { return m_name; } protected: std::string…
21
votes
1 answer

Compiler thinks that "A(A&)" accepts rvalues for a moment?

I have this code struct A { A(); A(A&); }; struct B { B(const A&); }; void f(A); void f(B); int main() { f(A()); } To my surprise this fails with GCC and Clang. Clang says for example Compilation finished with errors: source.cpp:8:10:…
Johannes Schaub - litb
  • 496,577
  • 130
  • 894
  • 1,212
21
votes
2 answers

Overload resolution behaviour difference between GCC and clang (SFINAE)

GCC accepts the following code: template struct meta { typedef typename T::type type; }; struct S {}; template typename meta::type foo(T, S); int foo(int, int); int main() { foo(0, 0); } But clang…
HighCommander4
  • 50,428
  • 24
  • 122
  • 194
20
votes
1 answer

Constructing a vector of structs (with some custom constructors) from exactly two string literals crashes. Why?

Can you guess the output of this trivial program? #include #include #include #include int main() { try { struct X { explicit X(int) {} X(std::string) {} // Just to…
Sz.
  • 3,342
  • 1
  • 30
  • 43
20
votes
1 answer

Why does {} as function argument not lead to ambiguity?

Consider this code: #include #include enum class A { X, Y }; struct Test { Test(const std::vector&, const std::vector& = {}, A = A::X) { std::cout << "vector overload" << std::endl; } Test(const…
Max Langhof
  • 23,383
  • 5
  • 39
  • 72
1 2
3
48 49