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

Overload resolution with multiple functions and multiple conversion operators

Consider simple code : #include struct A { operator double(){ std::cout<<"Conversion function double chosen."<
14
votes
2 answers

How does the assignment operator overload resolution work in this example? The result is unexpected for me

Here is the code that I do not understand: class Base { public: Base(){} Base operator=(Base ob2) { std::cout << "Using Base operator=() " << '\n'; return *this; } }; class Derived : public Base { public: …
Knitschi
  • 2,822
  • 3
  • 32
  • 51
14
votes
2 answers

Different casting operators used by different compilers

The following C++ program compiles without warnings in all compilers I have tried (gcc 4.6.3, llvm 3.0, icc 13.1.1, SolarisStudio 12.1/12.3): struct CClass { template operator T() const { return 1; } operator int() const { return 2;…
13
votes
3 answers

Single-Element-Vector Initialization in a Function Call

Consider the following example code: Example: void print(int n) { cout << "element print\n"; } void print(vector vec) { cout << "vector print\n"; } int main() { /* call 1 */ print(2); /* call 2 */ print({2}); std::vector
Imran
  • 642
  • 6
  • 25
13
votes
1 answer

Which of these conversions should be ambiguous?

I have some code like the following: class bar; class foo { public: operator bar() const; }; class bar { public: bar(const foo& foo); }; void baz() { foo f; bar b = f; // [1] const foo f2; bar b2 = f2; // [2] } GCC…
13
votes
1 answer

Why has this C++ code an ambiguous method call only on Microsoft compiler?

I'm trying to compile a library on microsoft C++ compiler 14.1 (Visual Studio 2017) but I'm getting a strange error due to an ambiguous call to a class method. After some testing I isolated the following code snippet: #include struct…
13
votes
2 answers

Determining which overload was selected

Let's say I have some arbitrary complicated overloaded function: template void foo(T&& ); template void foo(T* ); void foo(int ); I want to know, for a given expression, which foo() gets called. For example, given some macro…
Barry
  • 286,269
  • 29
  • 621
  • 977
13
votes
2 answers

Template conversion operator priority & constness

I have something along the lines of: #include class Foo; struct Test { template operator T() const // <----- This const is what puzzles me { std::cout << "Template conversion" << std::endl; …
chouquette
  • 971
  • 1
  • 7
  • 12
13
votes
2 answers

Initializer_list as argument to an array reference parameter in a not-template context

My question concerns this very simple and short code where an overload resolution is attempted between two non-template functions accepting an array reference parameter. The question has been posted elsewhere, but in a template deduction context.…
GSi
  • 649
  • 3
  • 10
13
votes
8 answers

overloading function and inheritance

I am trying to overload some template function to perform specific action if I call it using a given class MyClass or any derived class MyClassDer. Here is the code: #include struct MyClass { virtual void debug () const { …
12
votes
2 answers

Why aren't type constraints part of the method signature?

UPDATE: As of C# 7.3, this should no longer be an issue. From the release notes: When a method group contains some generic methods whose type arguments do not satisfy their constraints, these members are removed from the candidate set. Pre C#…
Daryl
  • 3,253
  • 4
  • 29
  • 39
12
votes
2 answers

Ambiguous overload error when using conversion function

I am trying to understand overloading resolution in C++ through the books listed here. One such example that i wrote to clear my concepts whose output i am unable to understand is given below. #include struct Name { operator int() …
12
votes
1 answer

Overload-Resolution: Is a direct conversion operator preferred (as a consequence of copy-elision)?

Given struct E { }; struct P { explicit P(E) {} }; struct L { operator E() {return {};} operator P() {return P{E{}};} }; According to the C++17 language standard, should the expression P{L{}} compile? Different compilers produce…
12
votes
1 answer

Function template overload resolution with two parameter packs

Consider the following code: #include template int f(T...) { return 1; } template int f(T...) { return 2; } int main() { std::cout << f(1); } It compiles and prints 1 on gcc 8.2, but fails to…
user10605163
12
votes
4 answers

How to resolve ambiguity of call to overloaded function with literal 0 and pointer

I'm pretty sure this must have been here already, but I didn't find much information on how to solve this kind of problem (without casting on the call): Given two overloads, I want that a call with function with a literal 0 always calls the unsigned…
mfya
  • 352
  • 4
  • 9