Why is it that no one writes with portability in mind in druntime?

Jonathan M Davis jmdavisProg at gmx.com
Mon Nov 25 02:34:52 PST 2013


On Monday, November 25, 2013 10:40:11 Joseph Rushton Wakeling wrote:
> On 25/11/13 09:52, Jonathan M Davis wrote:
> > Yes, they're aliases, but you have to worry about them. Take this code for
> > instance.
> > 
> > int len = arr.length;
> > 
> > That code will compile as 32-bit, but it will _not_ compile as 64-bit,
> > because length is size_t, which on 32-bit systems happens to be uint,
> > which implicitly converts to int, whereas on 64-bit systems, it's ulong,
> > which does _not_ implicitly convert to it.
> 
> You're quite right -- I was just thinking about underlying stuff along the
> lines of what Iain was pointing out for real, not "user-land" cases like
> this.
> 
> To be honest, I assumed that anyone with any experience or common sense
> would understand stuff like the example you've described, and not write
> such code. But that's probably very very naive ... :-)

Many people seem to just use int everywhere, and a lot of D code failed to 
compile when 64-bit support was finally added. It's probably less a problem now 
then it used to be, but it's still not all that uncommon to see code examples 
that get it wrong (particularly from Windows users, where using 64-bit is more 
of a pain). It happens all the time in C/C++ as well, but C/C++ will allow the 
conversion implicitly, so for better or worse, it works as long as the numbers 
aren't too large.

> The size_t case I rather like to think of is:
> 
>       size_t m = ...;
>       size_t p = 1 << m;    // should this be 1U, 1UL ... ?

I'd be _very_ suspicious of any bitshifts that aren't done on types with a 
fixed size.

- Jonathan M Davis


More information about the Digitalmars-d mailing list