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
12
votes
3 answers

Ambiguous call when recursively calling variadic template function overload

Consider this piece of code: template void foo() { } template void foo() { foo(); } int main() { foo(); return 0; } It does not compile due to…
rubix_addict
  • 1,811
  • 13
  • 27
12
votes
1 answer

Partially Specialized Structs vs Overloaded function template

As we know, function templates cannot be partially specialized in C++. When you are conceptually trying to achieve this, there are two possible solutions you can use. One of them is to use structs with a static function, optionally wrapped with a…
Nir Friedman
  • 17,108
  • 2
  • 44
  • 72
12
votes
1 answer

List-initialization and failed overload resolution of initializer_list constructor

The below fails to compile with clang35 -std=c++11: #include #include #include class A { public: A(int, bool) { std::cout << __PRETTY_FUNCTION__ << std::endl; } A(int, double) { std::cout <<…
Pradhan
  • 16,391
  • 3
  • 44
  • 59
12
votes
2 answers

Which to use: move assignment operator vs copy assignment operator

I don't seem to get why would you use the move assignment operator: CLASSA & operator=(CLASSA && other); //move assignment operator over, the copy assignment operator: CLASSA & operator=(CLASSA other); //copy assignment operator The move…
Kam
  • 5,878
  • 10
  • 53
  • 97
12
votes
1 answer

Method resolution issue with default parameters and generics

Using .NET 4, I am confused by the inability of the compiler to resolve the first method call in the sample below. using System; namespace MethodResolutionTest { class Program { static void Main(string[] args) { …
Kent Boogaart
  • 175,602
  • 35
  • 392
  • 393
12
votes
2 answers

How do function objects affect overload resolution?

Are function objects treated differently from regular functions during overload resolution? If so, how? I have run into the following case where replacing a function with an equivalently-callable function object changed the meaning of the…
HighCommander4
  • 50,428
  • 24
  • 122
  • 194
12
votes
1 answer

Wrong overload is overridden when two methods have identical signatures after substitution of type arguments

We believe this example exhibits a bug in the C# compiler (do make fun of me if we are wrong). This bug may be well-known: After all, our example is a simple modification of what is described in this blog post. using System; namespace…
11
votes
1 answer

Strange C# compiler behavior (overload resolution)

I've found very strange C# compiler behavior for following code: var p1 = new SqlParameter("@p", Convert.ToInt32(1)); var p2 = new SqlParameter("@p", 1); Assert.AreEqual(p1.Value, p2.Value); // PASS var x = 0; p1 = new…
Victor Haydin
  • 3,518
  • 2
  • 26
  • 41
11
votes
1 answer

Passing a concept-constrained function overload

The following code fails to compile (Godbolt link): #include template decltype(auto) g(Fn&& fn) { return fn(); } template requires(std::integral) int f() { return 0; } template int f() { return…
jcai
  • 3,448
  • 3
  • 21
  • 36
11
votes
1 answer

Array reference binding vs. array-to-pointer conversion with templates

This code sample fails to compile due to ambiguous overload resolution void g(char (&t)[4]) {} void g(char *t) {} int main() { char a[] = "123"; g(a); } and careful reading of overload resolution rules makes it clear why it fails. No problems…
AnT stands with Russia
  • 312,472
  • 42
  • 525
  • 765
11
votes
1 answer

Resolution of built-in operator == overloads

In the following code struct A has two implicit conversion operators to char and int, and an instance of the struct is compared for equality against integer constant 2: struct A { constexpr operator char() { return 1; } constexpr operator…
Fedor
  • 17,146
  • 13
  • 40
  • 131
11
votes
2 answers

Trouble with const/non-const overload resolution

I have a class that looks something like this: class ClassA { public: float Get(int num) const; protected: float& Get(int num); } Outside of the class, I call the Get() function. float foo = classAInstance.Get(i); I expect this to…
thekidder
  • 3,486
  • 1
  • 33
  • 36
11
votes
3 answers

Why does constructor choose type INT instead of SHORT when invoked with a parameter of type CHAR?

It's seen that in the below code the constructor with parameter of type int is being called. I know int is fine here. But why not short? as ASCII value of 'A' gives 65 which a short can accommodate. On what criteria the constructor with the…
Abhinav Kinagi
  • 3,653
  • 2
  • 27
  • 43
11
votes
1 answer

mypy error, overload with Union/Optional, "Overloaded function signatures 1 and 2 overlap with incompatible return types"

So, let's start with an example. Suppose we have several types that can be combined together, let's say we are using __add__ to implement this. Unfortunately, due to circumstances beyond our control, everything has to be "nullable", so we are forced…
juanpa.arrivillaga
  • 88,713
  • 10
  • 131
  • 172
11
votes
2 answers

Is generics bound part of method signature in Java?

I realized today that this compiles and runs fine: public class Test { public static T handle(T val) { System.out.println("T"); return val; } public static T handle(T val) { …
Xinchao
  • 2,929
  • 1
  • 24
  • 39