[Development] Upgrading the sources to C++11 keywords (Q_NULLPTR, etc.)

Matthew Woehlke mw_triad at users.sourceforge.net
Tue Feb 10 23:15:03 CET 2015


On 2015-02-08 16:42, André Pönitz wrote:
> I.e. in case of a simple pointer initialization, why should one *ever* prefer 
> 
> void something()
> {
>     Foo *f = Q_NULLPTR;
>     ...
> }
> 
> over
> 
> void something()
> {
>     Foo *f = 0;
>     ...
> }
> 
> ?

For the same reason that *any* style rule exists: to encourage standard
practices that make it easier to use compiler tools to find bugs.

If I religiously use Q_NULLPTR, than I can also turn on the warning that
tells me when I use '0' as a null pointer constant. Which in turn can
help catch places where I really meant '0' and *not* a null pointer but
an unintended conversion is occurring. Conversely, because 'nullptr' is
not an integer, you can't accidentally call something that takes an
integer when you really meant a pointer (doesn't apply so much to your
specific example, but is a reason to use it in general).

If '0' is permitted in some contexts, then it is hard to use the
warning, and therefore hard to catch bugs. (And, as mentioned, not using
it *in headers* can be particularly annoying to someone who wants to
employ such a policy downstream, and can't because Qt produces huge
amounts of warning spam. Granted, "in headers" should perhaps read "in
public macros", but still...)

As Bo noted, it's your own problem whether or not to use it in Qt's own
.cpp files. Please consider your users when deciding whether or not to
use it in headers, however.

-- 
Matthew




More information about the Development mailing list