error: call of overloaded ‘foo(int)’ is ambiguous (0 vs null ptr)

Peter A. Felvegi petschy@praire-chicken.com
Thu May 24 19:22:00 GMT 2012


Hello,

I'm not sure whether this is standard behaviour or not; nonetheless I 
was quite surprised:

----8<----8<----8<----
void foo(long);
void foo(const char*);

void bar()
{
         foo(0);
         foo(0+0); // !
         foo(1-1); // !
         foo(1);
}
----8<----8<----8<----

The first call to foo() in bar() is ambiguous, all right (integral or 
ptr), but the next two (marked w/ !) ? Shouldn't the compiler know that 
0+0 or 1-1, etc can't be a null pointer? I tried all minor versions from 
4.4 to 4.8.

Somewhat connected question: if I use |-Wzero-as-null-pointer-constant 
to detect cases where 0 means null pointer, shouldn't there be a switch 
that forbids conversion from 0 to null ptr? I have a class that has 
operator() overloaded for size_t and for const char* (access elements by 
index or by name). All is well, except for the index 0: the call would 
be ambiguous. I tried to trick this with 0+0, etc, hence the whole post. 
0.0 works, though. Any better ideas? Explicit cast?

Supposing that I eliminated all 0's that meant null ptr, using the 
switch could eliminate this kind of ambiguity, and I could use 0 where I 
mean 0, without any trickery.

Regards, Peter

|



More information about the Gcc mailing list