Here’s what I want to do:
const int64_t randomIntNumber = reinterpret_cast<int64_t> (randomUintNumber);
Where randomUintNumber is of type uint64_t.
The error is (MSVC 2010):
error C2440: ‘reinterpret_cast’ : cannot convert from ‘const uint64_t’
to ‘int64_t’ 1> Conversion is a valid standard conversion,
which can be performed implicitly or by use of static_cast, C-style
cast or function-style cast
Why doesn’t it compile? both types have the same bit length, isn’t it what reinterpret_cast is intended for?
Because that’s not what
reinterpret_castis for. All the permitted conversions withreinterpret_castinvolve pointers or references, with the exception that an integer or enum type can bereinterpret_castto itself. This is all defined in the standard,[expr.reinterpret.cast].I’m not certain what you’re trying to achieve here, but if you want
randomIntNumberto have the same value asrandomUintNumber, then doIf that results in a compiler warning, or if you just want to be more explicit, then:
The result of the cast has the same value as the input if
randomUintNumberis less than 263. Otherwise the result is implementation-defined, but I expect all known implementations that haveint64_twill define it to do the obvious thing: the result is equivalent to the input modulo 264.If you want
randomIntNumberto have the same bit-pattern asrandomUintNumber, then you can do this:Since
int64_tis guaranteed to use two’s complement representation, you would hope that the implementation definesstatic_castto have the same result as this for out-of-range values ofuint64_t. But it’s not actually guaranteed in the standard AFAIK.Even if
randomUintNumberis a compile-time constant, unfortunately hererandomIntNumberis not a compile-time constant. But then, how “random” is a compile-time constant? 😉If you need to work around that, and you don’t trust the implementation to be sensible about converting out-of-range unsigned values to signed types, then something like this:
Now, I’m in favour of writing truly portable code where possible, but even so I think this verges on paranoia.
Btw, you might be tempted to write this:
or equivalently:
This isn’t quite guaranteed to work, because although where they exist
int64_tanduint64_tare guaranteed to be a signed type and an unsigned type of the same size, they aren’t actually guaranteed to be the signed and unsigned versions of a standard integer type. So it is implementation-specific whether or not this code violates strict aliasing. Code that violates strict aliasing has undefined behavior. The following does not violate strict aliasing, and is OK provided that the bit pattern inrandomUintNumberis a valid representation of a value oflong long:So on implementations where
int64_tanduint64_tare typedefs forlong longandunsigned long long, then myreinterpret_castis OK. And as with the implementation-defined conversion of out-of-range values to signed types, you would expect that the sensible thing for implementations to do is to make them corresponding signed/unsigned types. So like thestatic_castand the implicit conversion, you expect it to work in any sensible implementation but it is not actually guaranteed.