This is the mail archive of the gcc@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

error: call of overloaded âfoo(int)â is amb iguous (0 vs null ptr)


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

|


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]