comma operator causes hard to spot bugs

Norbert Nemec Norbert at Nemec-online.de
Fri Apr 27 09:28:37 PDT 2012


I fully agree! I wonder why the comma operator made it into D at all. 
This would have been exactly the kind of nasty language details that D 
could have cleaned out.



On 21.04.2012 14:23, Benjamin Thaut wrote:
> Hi,
> I just had a bug due to the unitentional usage of the comma operator. So
> I'm wondering if there could something be done so that the compiler
> prevents something like this:
>
> memStart = cast(T*)(m_allocator.AllocateMemory(size * T.sizeof),
> InitializeMemoryWith.NOTHING);
>
> This first excutes AllocateMemory, then throws away the result and then
> casts the enum InitializeMemory.NOTHING value to a pointer, which will
> always result in a null pointer. This took me quite some time to spot.
> What I actually wanted to do:
>
> memStart = cast(T*)(m_allocator.AllocateMemory(size * T.sizeof,
> InitializeMemoryWith.NOTHING));
>
> 1) So is it actually neccessary that enums can be casted directly to
> pointers?
> 2) Is the functionality provided by the comma operator actually worth
> the bugs it causes?
>
> Kind Regards
> Benjamin Thaut



More information about the Digitalmars-d mailing list