On 24 May 2012 20:26, Peter A. Felvegi wrote:
> Hello,
>
> I'm not sure whether this is standard behaviour or not; nonetheless I was
> quite surprised:

Please note that "is this valid" questions are not appropriate on this
mailing list, please use the gcc-help mailing list for questions about
using GCC or a general C++ forum, thanks.

There are (at least) two high-quality online compilers you can use to
see whether something you find surprising is a GCC bug or not:

http://comeaucomputing.com/tryitout/
http://llvm.org/demo/

They both agree with GCC.

> 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?

IMHO no. The warning issues a diagnostic that may be desirable for
style reasons, your suggestion alters the language and makes any
program relying on it non-portable.

You can make it an error with -Werror=zero-as-null-pointer-constant
but that doesn't resolve any ambiguity.

> 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?

Yes,an explicit cast works, or a literal that doesn't require
conversion, e.g. 0ul (assuming size_t is a synonym for unsigned long)
or an enumerator with value zero which would only allow conversion to
size_t, or in C++11 use the keyword nullptr.

Reply via email to