This is the mail archive of the
gcc@gcc.gnu.org
mailing list for the GCC project.
Re: No pointer conversion warning for "bool" in C/C++
- From: Jonathan Wakely <jwakely dot gcc at gmail dot com>
- To: Ian Lance Taylor <iant at google dot com>
- Cc: Jon Grant <jg at jguk dot org>, gcc at gcc dot gnu dot org
- Date: Mon, 26 Sep 2011 08:10:44 +0100
- Subject: Re: No pointer conversion warning for "bool" in C/C++
- References: <4E7FA590.8020501@jguk.org> <mcrd3eokjub.fsf@coign.corp.google.com>
On 26 September 2011 05:29, Ian Lance Taylor wrote:
> Jon Grant <jg@jguk.org> writes:
>
>> Currently gcc, and g++ don't give a warning when a pointer was
>> converted to a bool, in the same way it is for other types.
At least in C++, it's not really true to say "in the same way it is
for other types" because you cannot convert from a pointer to any
integer type except bool. Your test uses NULL for the other integer
types, which is an integral constant expression with value zero, so
it's ok to convert that to an integer type. That's not true for
general pointer values: if your test used m_int(g_glob) then it
wouldn't compile.
> There is a lot of code which uses
> ? ?if (p)
> where p is a pointer to test whether p is not NULL. ?I don't think we
> could reasonably have gcc warn about such a case.
>
> We might be able to separate out conversion to bool on assignment from
> conversion to bool in a test, though.
That would still break this:
Base* p = getObj();
bool is_derived = dynamic_cast<Derived*>(p);
What problem is the warning supposed to solve?