views:

40

answers:

1

I have an existing C codebase that works on x86.

I'm now compiling it for x64.

What I'd like to do is cast a size_t to a DWORD, and throw an exception if there's a loss of data.

Q: Is there an idiom for this?


Here's why I'm doing this:

A bunch of Windows APIs accept DWORDs as arguments, and the code currently assumes sizeof(DWORD)==sizeof(size_t). That assumption holds for x86, but not for x64. So when compiling for x64, passing size_t in place of a DWORD argument, generates a compile-time warning.

In virtually all of these cases the actual size is not going to exceed 2^32. But I want to code it defensively and explicitly.

This is my first x64 project, so... be gentle.

+1  A: 

see boost::numeric_cast

http://www.boost.org/doc/libs/1_33_1/libs/numeric/conversion/doc/numeric_cast.html

Pavel Radzivilovsky
You know that boost is at like version 1.43 already, right?
Nikolai N Fetissov
yeah.. welcome to edit the link
Pavel Radzivilovsky
http://www.boost.org/doc/libs/1_43_0/libs/numeric/conversion/doc/html/boost_numericconversion/improved_numeric_cast__.html
smerlin