Consider the following code sample:
void my_print(bool is_red, const char* format, ...){
va_list args;
va_start(args, format);
if(is_red)
print_red(format, args);
else
print_normal(format, args);
va_end(args);
}
void my_print(const char* format, ...){
va_list args;
va_start(args, format);
print_normal(format, args);
va_end(args);
}
int main() {
my_print((const char*)"Hello %s\n", "World");
return 42;
}
This code is ambiguous to the compiler and it yields the error:
more than one instance of overloaded function "my_print" matches the argument list: function "my_print(bool is_red, const char *format, ...)" (declared at line 12) function "my_print(const char *format, ...)" (declared at line 22) argument types are: (const char *, const char [6])
From what I understand the parameters I passed can interpreted as either (const char*, ...) where the '...' is just 'const char*' or (bool, const char*, ...) where the '...' is empty.
I would expect the compiler to assume I want to call the one which does not require an implicit cast from 'bool' to 'const char*' (which I do), and hence call the second instance.
How can I clarify this ambiguity to the compiler without changing the syntax of the function call?