Questions tagged [ambiguous-call]

Ambiguous call is a situation, when compiler cannot deduce from the passed parameter types, which version of function or method shall it use.

Ambiguous call happens, when the parameters can be converted in many ways, such that they fit to more than one overload of a function. For example:

void f(float f)
{

}

void f(double d)
{

}

Passing an int value to this function will cause an ambiguous call compiler error, because int can be converted both to float and to double while the compiler is not able to choose the overloaded version of function uniquely.

102 questions
9
votes
3 answers

Question about ambiguous calls in C#

I have a question that's not really a problem, but something that made me a little curious. I have a class with two methods in it. One is a static method and the other one is an instance method. The methods have the same name. public class…
Patrik Svensson
  • 13,536
  • 8
  • 56
  • 77
9
votes
1 answer

static_cast not working on precedence as expected

#include #include template void foo() { std::cout << "a" << std::endl; } template void foo() { std::cout << "b" << std::endl; } int main() { foo(42)> (); …
user2485710
  • 9,451
  • 13
  • 58
  • 102
8
votes
1 answer

Upcast mandatory when there are different overload

This is not about windows forms at all it's here only for the "background". I was toying around Windows Forms when I got an error on an AddRange for a MenuStrip.Items requiring to cast ToolStripMenuItem into ToolStripItem But I already have an…
Sehnsucht
  • 5,019
  • 17
  • 27
7
votes
2 answers

Disambiguating list initialization for std::vector

I have an overloaded function in my code with the type signatures: void foo(std::string); void foo(std::vector); I would like the user of foo to be able to call it with either a string or a list of strings //Use case…
Ross
  • 567
  • 1
  • 4
  • 8
7
votes
2 answers

Why does this not produce an ambiguity?

I just wrote some code with the following structure: public void method(int x) { //... } public void method(int x, String... things) { //... } I was rather surprised that this compiled, and that if I invoked method(3); then it would pick…
chiastic-security
  • 20,430
  • 4
  • 39
  • 67
7
votes
6 answers

Ambiguous function/constructor call in C#

The following code causes a compiler error, as it is ambiguous call but the problem if we use object instead of ArrayList no error happens and the string version works fine; Do you have an explanation for that? class A { public A(string x) …
Ahmed
  • 7,148
  • 12
  • 57
  • 96
7
votes
3 answers

Why doesn't private inheritance resolve ambiguity for static functions ? (tested in MSVC)

I'm wondering why a call to a static function is ambiguous, even when one of the two is obviously impossible to call as it is private. I was hoping I could use private / protected inheritance to help the compiler solve the ambiguity. Is it specific…
N0vember
  • 995
  • 1
  • 9
  • 12
7
votes
1 answer

Why aren't these overloads ambiguous?

The following code compiles fine with gcc and clang. template struct identity { typedef T type; }; template void foo(typename identity::type); template void foo(T); int main() { foo(0); } It…
HighCommander4
  • 50,428
  • 24
  • 122
  • 194
6
votes
0 answers

How to check whether, given the argument types, an implicit use of `operator ()` would result in exactly one best viable candidate?

As I understand it, the outcome of a function name usage might be one of the following: There are no (best) viable functions — overload resolution fails. The suboutcomes are: There are no candidates. There are some candidates, just none are…
6
votes
2 answers

C++ partial template argument deduction for function with variadic pack produces ambiguous call in Clang and MSVC

Consider the following snippet (available on compiler epxlorer): template auto foo(Args&&... args) {} template auto foo(Args&&... args) {} int main() { foo('a'); } It compiles perfectly…
Naebaf
  • 321
  • 1
  • 8
5
votes
2 answers

How to resolve ambigiously named extension method?

I have a DataTable that I'm trying to enumerate over with the AsEnumerable extension method on System.Linq.Enumerable. The problem is that there is an identically named extension method on System.Data.DataTableExtensions. I need to use both…
Jace Rhea
  • 4,982
  • 4
  • 36
  • 55
5
votes
2 answers

ISO C++ says that these are ambiguous,

I have to overload the shift operator " << " both for writing in console and to write on a binary file.. I am doing okay for the ostream overloading, while I am having some problem overloading the fstream, here it is: in my header: friend ostream…
arocketman
  • 1,134
  • 12
  • 21
4
votes
1 answer

Function overload resolution with nullptr as argument

Consider the code below. Although both overloads of fun accept pointers, passing nullptr to fun does not result in any compilation error. Whereas, the very similar function bun fails to compile. When I print the the types of the argument i using…
ozlsn
  • 144
  • 4
4
votes
3 answers

Disambiguation of friend and member binary operator

Consider the following class with a binary operator (I use operator+ just as an example). struct B{}; template struct A{ template void operator+(BB const&) const{std::cout<<"member"<
alfC
  • 14,261
  • 4
  • 67
  • 118
4
votes
2 answers

What makes a Min(byte,int) call ambiguous?

I do not understand why the following is ambiguous according to compiler: byte x = 200; int novaCervena = Math.Min(x, 10); And once I add +1 to byte it is not byte x = 200; int novaCervena = Math.Min(x+1, 10);
lojol
  • 471
  • 1
  • 5
  • 9