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

Overload resolution of constructors for different classes

Consider this code. struct A { int i; }; struct B { char c; }; struct C { double d; }; void f(A a); void f(B b); void f(C c); void g() { f({5}); } Here I get an ambiguity in f({5});. But it seems that struct A's constructor is…
10
votes
3 answers

Explicit constructors and nested initializer lists

The following code successfully compiles with most modern C++11 compatible compilers (GCC >= 5.x, Clang, ICC, MSVC). #include struct A { explicit A(const char *) {} A(std::string) {} }; struct B { B(A) {} …
Ext3h
  • 5,713
  • 17
  • 43
10
votes
1 answer

C++ template overload resolution called from template function pick candidate declared after template instantiation

Here is a minimalist example of a very strange overload resolution from a template context: #include // Types // struct I { int v; }; template struct D { T t; }; // Functions // // Overload 1 template I f(T) {…
Mat-Tso
  • 103
  • 5
10
votes
2 answers

Kotlin: Inline lambda and overload resolution ambiguity

I have a simple factory pattern where the implementation is determined through overload resolution. Problem is that the Kotlin compiler complains with "Overload resolution ambiguity.." for the inline lambda. class Foo(){ companion object Factory…
Tomas Karlsson
  • 705
  • 1
  • 6
  • 17
10
votes
2 answers

Overload resolution: assignment of empty braces

I wrote some code S s; ... s = {};, expecting it to end up the same as S s = {};. However it didn't. The following example reproduces the problem: #include struct S { S(): a(5) { } S(int t): a(t) {} S &operator=(int t) { a…
M.M
  • 138,810
  • 21
  • 208
  • 365
10
votes
1 answer

Force `const char[]` string literals in clang

Compiling the following code void f(char *, const char *, ...) {} void f(const char *, ...) {} int main() { f("a", "b"); } with clang gives me this error: prog.cpp:6:2: error: call to 'f' is ambiguous f("a", "b"); …
Yakov Galka
  • 70,775
  • 16
  • 139
  • 220
10
votes
3 answers

Wrong overload giving compiler error

Using VS2013, in the following example two different errors are given when attempting to pass a function to a worker's constructor, yet, lambda functions with the same prototype are ok. What am I doing wrong, and how can I change the definition of…
Steve
  • 613
  • 5
  • 15
10
votes
3 answers

Overload resolution with CType'd Enum

Consider the following minimal example: Module Module1 Private Enum MyEnum A End Enum Public Sub Main(args As String()) AreEqual(CType(0, MyEnum), MyEnum.A) ' Error here End Sub Private Function AreEqual(Of…
Heinzi
  • 167,459
  • 57
  • 363
  • 519
10
votes
2 answers

Why does the first function call bind to the first function?

Why does the first function call (cm(car);) bind to the first function? I understand that second call is bound to second function because it's non-template, despite both being perfect matches. If the first function is defined as non-template with…
10
votes
1 answer

Lambda conversions with unclear return type and overload resolution

If I have a lambda such as () => { throw new Exception(); }, it's unclear whether it has a return type or not. Because of this, it can be (implicitly) converted to both Action and Func (or any other Func). This is because, according to…
svick
  • 236,525
  • 50
  • 385
  • 514
10
votes
2 answers

Why are const qualifiers in function arguments used for overloading resolution?

Possible Duplicate: Functions with const arguments and Overloading I am pretty confused by the overloading and const declaration rules. Here are two things that puzzle me maybe you can help me find the deeper misunderstanding in my head that…
Sarien
  • 6,647
  • 6
  • 35
  • 55
9
votes
2 answers

Strange case of C++11 overload resolution

I came across a rather strange case of overload resolution today. I reduced it to the following: struct S { S(int, int = 0); }; class C { public: template C(S, Args... args); C(const C&) = delete; }; int…
HighCommander4
  • 50,428
  • 24
  • 122
  • 194
9
votes
1 answer

Overload resolution for function templates with auto non-type template parameter

What are the rules for the selection of overloaded function templates in case of non-type template parameters, if one of the parameters is a placeholder type. I am confused with the current behavior of the compilers, consider the next…
Fedor
  • 17,146
  • 13
  • 40
  • 131
9
votes
1 answer

Overload resolution between conversion operators to value and to const-reference in C++

In the following program struct B defines two conversion operators: to A and to const A&. Then A-object is created from B-object: struct A {}; struct B { A a; B() = default; operator const A&() { return a; } operator A() { return a;…
Fedor
  • 17,146
  • 13
  • 40
  • 131