Overload, variational functions and bool type

After compilation, the program works fine and works as expected. Its output:

one
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); } 

Now, if I changed the int parameter of the second Bar function to bool and foo1.Bar(1, "xx", "xx", 2, 2); on foo1.Bar(true, "xx", "xx", 2, 2); , the following line will not compile, and I get an error: 'Foo::Bar': 2 overloads have similar conversions :

  foo1.Bar("Test", "xx", 1, 2); 

The whole program that does not compile:

 #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); } 

I do not understand why in the second case there is ambiguity.

EDIT

But if pointers are implicitly converted to bool , then why compile?

 #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); } 
+8
c ++ c ++ 11
source share
2 answers

When you match "Test", "xx", 1, 2 with const char*, ... , the conversion sequence for the first argument has an exact match rank, the second and fourth for the ellipsis conversion sequence. So (exact match, ellipsis, ellipsis, ellipsis).

When you match "Test", "xx", 1, 2 with bool, const char*, ... , the first conversion sequence for the first argument has the conversion rank; the second is an exact match; the third and fourth are the ellipsis conversion sequences. In other words, (conversion, exact match, ellipsis, ellipsis).

Exact match; everything beats with an ellipsis (see [over.ics.rank] ). Thus, we have a so-called criss-cross situation where one function has the best conversion sequence for one argument and the other function has the best conversion sequence for the other argument. Since a necessary (but not sufficient) condition for a function is better than another function is that none of the conversion sequences is worse than the function of the other function ( [over.match.best] / 1 ), neither of these two functions is better others.

+5
source share

This is normal:

 #include <stdio.h> class Foo { public: void Bar(const char* b, ...) { printf("1\n"); }; void Bar(bool a, ...) { printf("2\n"); }; }; int main() { Foo foo; foo.Bar("Test"); } 

This not normal:

 #include <stdio.h> class Foo { public: void Bar(const char* b, ...) { printf("1\n"); }; void Bar(bool a, char another, ...) { printf("2\n"); }; }; int main() { Foo foo; foo.Bar("Test", 'c'); // Ambiguous! } 

In the first case, the first version of Bar () is clearly better. But in the second case, this is not so clear, because although the first version has a better match for parameter 1, the second version has a better match for parameter 2.

The question remains: why does using "int" instead of "bool" avoid ambiguity? The answer is that a pointer can implicitly convert to bool, but not int.

+5
source share

All Articles