On Tue, Jan 26, 2016 at 8:21 AM, Stephan Beal <sgbeal at googlemail.com> wrote:

> On Tue, Jan 26, 2016 at 3:47 PM, J Decker <d3ck0r at gmail.com> wrote:
>
> > should be (size_t) instead of (int) though... since size_t will retain
> > the precision... and then back propagate the change to the function
> > return type and the things receiving the return... then you don't need
> > the cast anyway.
> >
>
> fwiw, in case this matters: size_t has an unspecified size and it's not in
> C89. It's defined by C99 in stddef.h
>

size_t (and ptrdiff_t) are both in C89/C90 in stddef.h. Really ptrdiff_t is
what one wants for the difference between two pointers. Their size is
platform specified.

As far as casting goes, I dislike casting unless absolutely necessary. I
will do it to shut up compiler warnings if there is nothing better and I
can guarantee that the difference can never exceed the size of the
destination type, and perhaps that guarantee is possible here. Too often
(not in SQLite necessarily but in other projects) I see people casting
without consideration to the size requirements just because of the warning.

Given that most of my code is C++, I really like the numeric_cast. It
inlines to a normal cast in cases where there is no danger (non-truncating
conversions) and can throw an exception in the case of a truncating or
signed conversion that is out of range. Sadly, something like that isn't
possible in pure C89.

-- 
Scott Robison

Reply via email to