[Development] 0 vs. NULL

René J. V. Bertin rjvbertin at gmail.com
Tue Oct 13 14:26:10 CEST 2015


Pocheptsov Timur wrote:

Well, I know that (as in, I've been told it's the case, and I've seen examples 
where you cannot do regular, static casts), but that doesn't mean I understand 
it (nor does it mean I'm not willing to believe it!!).

Worse, experience with ObjC interferes. There you can cast like in C (at least 
the last time I tried :)), and once you know that that address you're casting 
around corresponds to a data structure that contains the type information, it 
becomes trivial to understand why something nonsensical like [(NSString*)nsWin 
someMessage] might compile without warning but not at all behave like you'd 
expect.

I've also done my share of writing C glue for C++ libraries, and typically 
export C++ instances as void* variables to and back from C code. Until now that 
has never bitten me...

R.

> Well, that's a static type system.
> 
> I would explain this 'asymmetry' as:
> 
> some_pointer_type -> pointer_to_void conversion is safe (given that it's
> possible at all, since not every pointer type can be implicitly converted to
> void *).
> 
> while hypothetical (non-existent) implicit
> 
> pointer_to_void -> some_pointer_type
> 
> IS NOT, and compiler, issuing error messages will help you to avoid some
> troubles.
> 
> ________________________________________
> From: René J.V. Bertin <rjvbertin at gmail.com>
> Sent: Friday, October 09, 2015 5:51 PM
> To: Pocheptsov Timur
> Cc: development at qt-project.org
> Subject: Re: [Development] 0 vs. NULL
> 
> On Friday October 09 2015 13:58:37 Pocheptsov Timur wrote:
> 
>> if NULL was (void *)0 - you'd have a compilation error in C++, since there is
>> no such implicit conversion.
>>
>> And yes, void * in C++ can be indeed considered generic, because you can do
>> this:
>>
>> int * p = ...
>> void * pv = p;
> 
> Coming from C I have some trouble with that concept of generic pointer. Once
> you're used that it implies not only "can be assigned to any type of pointer"
> but also "can be assigned any type of pointer" it's not so easy to remember
> which of the 2 properties has been dropped. I don't see any evident reason why
> in the above example it should be possible to assign p to pv without an
> explicit cast, but not the other way round.
> 
> R.





More information about the Development mailing list