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

Overloaded function template disambiguation with `std::enable_if` and non-deduced context

Consider the following code: template struct dependent_type { using type = T; }; template auto foo(T) -> std::enable_if_t{}> { std::cout << "a\n"; } template void foo(typename…
Vittorio Romeo
  • 90,666
  • 33
  • 258
  • 416
16
votes
2 answers

SFINAE not happening with std::underlying_type

Below SFINAE code with variadic templates compiles nicely using clang 3.7.1, C++14: #include #include #include #include enum class Bar : uint8_t { ay, bee, see }; struct S { static void foo() {} //…
Paolo M
  • 12,403
  • 6
  • 52
  • 73
16
votes
3 answers

SFINAE away a copy constructor

Under certain conditions, I'd like to SFINAE away the copy constructor and copy assignment operator of a class template. But if I do so, a default copy constructor and a default assignment operator are generated. The SFINAE is done based on tags I…
user1095108
  • 14,119
  • 9
  • 58
  • 116
16
votes
2 answers

Why can't the compiler tell the better conversion target in this overload resolution case? (covariance)

Understanding the C# Language Specification on overload resolution is clearly hard, and now I am wondering why this simple case fails: void Method(Func f) { } void Method(Func f) { } void Call() { Method(() => { throw new…
Jeppe Stig Nielsen
  • 60,409
  • 11
  • 110
  • 181
16
votes
1 answer

Why does the compiler find my function if is not yet declared?

Contrary to my expectations, this program works: #include namespace a { struct item{}; } namespace b { struct item{}; } template void func(T t) { do_func(t); } int main() { func(a::item{}); …
StackedCrooked
  • 34,653
  • 44
  • 154
  • 278
15
votes
1 answer

Overload resolution for a function with multiple parameter packs in C++

A C++ function can have more than one parameter packs. Although it does not look very practical thing, it is still interesting to know language rules about them. For example, in case of two overloads: constexpr int f(auto...) { return 1; } constexpr…
Fedor
  • 17,146
  • 13
  • 40
  • 131
15
votes
4 answers

Java: runtime method resolution

I'm working on some dynamic invocation of code via an interpreter, and I'm getting into the sticky ugly areas of method resolution as discussed in JLS section 15.12. The "easy" way to choose a method is when you know the exact types of all the…
Jason S
  • 184,598
  • 164
  • 608
  • 970
15
votes
4 answers

C++0x confusion with using declarations

What should happen for this case: struct A { void f(); }; struct B : virtual A { using A::f; }; struct C : virtual A { using A::f; }; struct D : B, C { void g() { f(); } }; The line of interest is f(). Clearly the lookup of f…
Johannes Schaub - litb
  • 496,577
  • 130
  • 894
  • 1,212
15
votes
1 answer

Ambiguity in parameter type inference for C# lambda expressions

My question is motivated by Eric Lippert's this blog post. Consider the following code: using System; class Program { class A {} class B {} static void M(A x, B y) { Console.WriteLine("M(A, B)"); } static void Call(Action f) {…
Bartosz
  • 461
  • 2
  • 9
15
votes
2 answers

C++ 11: overload resolution and SFINAE

I'm learning SFINAE and this is my first attempt to print "YES" only for those types you can output with std::ostream (forget about std::operator<<(std::ostream &, T) for now...): template void f(const T &) { std::cout << "NO" <<…
nodakai
  • 7,773
  • 3
  • 30
  • 60
14
votes
2 answers

How to perfectly forward `*this` object inside member function

Is it possible to perfectly forward *this object inside member functions? If yes, then how can we do it? If no, then why not, and what alternatives do we have to achieve the same effect. Please see the code snippet below to understand the question…
parth_07
  • 1,322
  • 16
  • 22
14
votes
3 answers

Why const char* implicitly converted to bool rather than std::string?

#include #include struct mystruct{ mystruct(std::string s){ std::cout<<__FUNCTION__ <<" String "<
14
votes
1 answer

Why does C# compiler overload resolution algorithm treat static and instance members with equal signature as equal?

Let we have two members equal by signature, but one is static and another - is not: class Foo { public void Test() { Console.WriteLine("instance"); } public static void Test() { Console.WriteLine("static"); } } but such code generate…
abatishchev
  • 98,240
  • 88
  • 296
  • 433
14
votes
3 answers

Overload resolution of template functions

Consider this code: #include //Number1 template auto max (T1 a, T2 b) { std::cout << "auto max(T1 a, T2 b)" <
DamissaK
  • 165
  • 7
14
votes
1 answer

Overloading conversion function templates

Consider the following: struct X { template operator T(); // #1 template operator T&(); // #2 }; int a = X{}; // error: ambiguous int& b = X{}; // calls #2 int const& c = X{}; // calls #2 The situation for…
Barry
  • 286,269
  • 29
  • 621
  • 977