C++ null pointer conversions (was Re: Keeping binary compatibility)

Lubos Lunak l.lunak at suse.cz
Tue Oct 5 16:01:09 BST 2010


On Tuesday 05 of October 2010, Thiago Macieira wrote:
> Em Terça-feira 05 Outubro 2010, às 14:21:58, Lubos Lunak escreveu:
> >  Thanks for saving me the work of refuting your first paragraph by doing
> > so in your second paragraph :). And note that at least with gcc NULL
> > already is nullptr in practice.
>
> C++ is also happy to convert from pointer to bool if it can. That's why we
> added the private constructor QVariant::QVariant(void*) to Qt 4.7.
>
> The worst part in my opinion is that C++ also happily converts "false" to
> pointer. (false is 0 and 0 can be a pointer)
>
> $ g++ -fsyntax-only -xc++ - <<<'void foo(void *); int main() { foo(false);
> }';

$ g++ -fsyntax-only -xc++ - <<<'void foo(void *); int main() { foo(false); }'
<stdin>: In function ‘int main()’:
<stdin>:1:41: warning: converting ‘false’ to pointer type for argument 1 
of ‘void foo(void*)’
$ g++ --version
g++ (SUSE Linux) 4.5.0 20100604 [gcc-4_5-branch revision 160292]
...

And, BTW (it took only slightly less than 2 years to make gcc developers see 
such a warning could be useful by default):
$ echo -e '#include <stdio.h>\n void foo(int); int main() { foo(0); }' | 
g++ -fsyntax-only -xc++ -
$ echo -e '#include <stdio.h>\n void foo(int); int main() { foo(NULL); }' | 
g++ -fsyntax-only -xc++ -
<stdin>: In function ‘int main()’:
<stdin>:2:38: warning: passing NULL to non-pointer argument 1 of ‘void 
foo(int)’

-- 
 Lubos Lunak
 l.lunak at suse.cz




More information about the kde-core-devel mailing list