Folgende Programm kompiliert gut und funktioniert wie erwartet. Sein Ausgang ist:Überladen, variadic Funktionen und Bool-Typ
1
2
#include <stdio.h>
class Foo
{
public:
void Bar(const char* b, ...) { printf("1\n"); };
void Bar(int a, const char* b, ...) { printf("2\n"); };
};
int main()
{
Foo foo1;
foo1.Bar("Test", "xx", 1, 2);
foo1.Bar(1, "xx", "xx", 2, 2);
}
Nun, wenn ich den int
Parameter der zweiten Bar
Funktion in bool
und foo1.Bar(1, "xx", "xx", 2, 2);
in foo1.Bar(true, "xx", "xx", 2, 2);
ändern, wird die folgende Zeile nicht kompiliert und ich erhalte die Fehlermeldung: 'Foo::Bar': 2 overloads have similar conversions
:
foo1.Bar("Test", "xx", 1, 2);
Das gesamte Programm, das nicht kompiliert:
#include <stdio.h>
class Foo
{
public:
void Bar(const char* b, ...) { printf("1\n"); };
void Bar(bool a, const char* b, ...) { printf("2\n"); };
};
int main()
{
Foo foo1;
foo1.Bar("Test", "xx", 1, 2); // error: 'Foo::Bar': 2 overloads have similar conversions
foo1.Bar(true, "xx", "xx", 2, 2);
}
Ich verstehe nicht, warum es im zweiten Fall eine Zweideutigkeit gibt.
EDIT
Aber wenn Zeiger implizit auf bool
konvertieren, warum kompilieren lässt folgendes?
#include <stdio.h>
class Foo
{
public:
void Bar(const char* b) { printf("1\n"); };
void Bar(bool a) { printf("2\n"); };
};
int main()
{
Foo foo1;
foo1.Bar("Test");
foo1.Bar(true);
}
Ähnliche Fragen http://stackoverflow.com/questions/26413951/overloaded-bool-string-ambiguity – robor78
Dies hat nichts mit variadischen Funktionen zu tun, die Sie entdeckt hätten, wenn Sie einen _minimal_ Testfall erstellt hätten. –
@LightnessRacesinOrbit Ich habe gerade die Frage bearbeitet und einen minimalen Testfall ohne variadische Funktionen hinzugefügt, der ... –