Question

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?

Was it helpful?

Solution

Because that's not what reinterpret_cast is for. All the permitted conversions with reinterpret_cast involve pointers or references, with the exception that an integer or enum type can be reinterpret_cast to 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 randomIntNumber to have the same value as randomUintNumber, then do

const int64_t randomIntNumber = randomUintNumber;

If that results in a compiler warning, or if you just want to be more explicit, then:

const int64_t randomIntNumber = static_cast<int64_t>(randomUintNumber);

The result of the cast has the same value as the input if randomUintNumber is less than 263. Otherwise the result is implementation-defined, but I expect all known implementations that have int64_t will define it to do the obvious thing: the result is equivalent to the input modulo 264.


If you want randomIntNumber to have the same bit-pattern as randomUintNumber, then you can do this:

int64_t tmp;
std::memcpy(&tmp, &randomUintNumber, sizeof(tmp));
const int64_t randomIntNumber = tmp;

Since int64_t is guaranteed to use two's complement representation, you would hope that the implementation defines static_cast to have the same result as this for out-of-range values of uint64_t. But it's not actually guaranteed in the standard AFAIK.

Even if randomUintNumber is a compile-time constant, unfortunately here randomIntNumber is 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:

const int64_t randomIntNumber = 
    randomUintNumber <= INT64_MAX ? 
        (int64_t) randomUintNumber :
        (int64_t) (randomUintNumber - INT64_MAX - 1) + INT64_MIN;

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:

const int64_t randomIntNumber = reinterpret_cast<int64_t&>(randomUintNumber);

or equivalently:

const int64_t randomIntNumber = *reinterpret_cast<int64_t*>(&randomUintNumber);

This isn't quite guaranteed to work, because although where they exist int64_t and uint64_t are 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 in randomUintNumber is a valid representation of a value of long long:

unsigned long long x = 0;
const long long y = reinterpret_cast<long long &>(x);

So on implementations where int64_t and uint64_t are typedefs for long long and unsigned long long, then my reinterpret_cast is 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 the static_cast and the implicit conversion, you expect it to work in any sensible implementation but it is not actually guaranteed.

OTHER TIPS

Use static_cast in these cases. I suppose the language designers in all their wisdom decided that it's not considered "unsafe enough" to warrant a reinterpret_cast.

No, it is not. reinterpret_cast is mostly intended to reinterpret an existing bit of storage as a different type than it is. A lot of these interpretations is implementation dependent, and the standard lists a specific (rather long to quote here) list of things that can be done with a reinterpret_cast (mostly casting between different pointer/reference types), but says:

No other conversion can be performed explicitly using reinterpret_cast.

In your case, you probably want a conversion of types, not a reinterpretation of existing storage. Use static_cast for this purpose.

From C++11 Standard(N3376) 5.2.10.1 this document, page 101:

Conversions that can be performed explicitly using reinterpret_cast are listed below. No other conversion can be performed explicitly using reinterpret_cast.

The only conversion from integral type to integral type allowed is infact if the types are identical.

Others involve pointers.

Why doesn't it compile?

Because neither type is a pointer.

both types have the same bit length,

Why would that matter? If they were pointers, maybe it would matter that they pointed to things of the same size, but they're not pointers.

isn't it what reinterpret_cast is intended for?

No, reinterpret_cast is for pointer casts. You could do what you want by putting an & inside the cast and a * outside it. That's what reinterpret cast is for.

reinterpret_cast is used for reinterpreting the storage of the object as a different object. If you don't want to go through standard wording, you can find all that reinterpret_cast can do here. Generally you can remember you have to work with pointer types.

So, if you really want to reinterpret the bits used for your uint64_t as int64_t, then do this:

int64_t randomIntNumber = reinterpret_cast<int64_t&> (randomUintNumber);

However if you just want to convert the object, preserving its value if possible ... just do what the compiler suggests and use static_cast instead.

Reinterpret bit pattern with memcpy, transform type with static cast

Using reinterpret_cast you are violating strict aliasing. This is bad, as it ends up with undefined behavior (maybe you code malfunctions with a new compiler version.) What is the Strict Aliasing Rule and Why do we care? is a great article, describing the problem and its solution (maybe skip the lengthy part "Now, to the Rule-Book" ;) ). It recommends memcpy and argues, that compiler optimization will skip the copying anyway.

code

Interactive code here. In your specific case, all options yield the same result. This changes, when playing with newType and randomUintNumber.

#include <iostream>
#include <cstring>

int main()
{
    typedef int64_t newType; // try: double, int64_t
    
    uint64_t randomUintNumber = INT64_MAX + 10000; // try: 64000, INT64_MIN, INT64_MAX, UINT64_MAX, INT64_MAX + 10000
    std::cout << "INT64_MIN: " << INT64_MIN << std::endl;
    std::cout << "UINT64_MAX: " << UINT64_MAX << std::endl;
    std::cout << "INT64_MAX: " << INT64_MAX << "\n\n";
    std::cout << "randomUintNumber: " << randomUintNumber << "\n\n";
    
    // const int64_t randomIntNumber = reinterpret_cast<int64_t> (randomUintNumber);
    std::cout << "as \"const int64_t randomIntNumber = reinterpret_cast<int64_t> (randomUintNumber);\" does not work, here are some options ...\n\n";
    
    std::cout << "BAD [undefined behavior!]:" << std::endl;
    const newType a = reinterpret_cast<int64_t&> (randomUintNumber);
    std::cout << "\treinterpret_cast<int64_t&> (randomUintNumber): " << a << std::endl;
    
    const newType b = reinterpret_cast<int64_t&&> (randomUintNumber);
    std::cout << "\treinterpret_cast<int64_t&&> (randomUintNumber): " << b << std::endl;
    
    std::cout << "\nGOOD: " << std::endl;
    const newType c = (int64_t) randomUintNumber;
    std::cout << "\t(int64_t) randomUintNumber [static cast, sometimes reinterprets bit pattern]: " << c << std::endl;
    
    const newType d = static_cast<int64_t>(randomUintNumber);
    std::cout << "\tstatic_cast<int64_t>(randomUintNumber) [the same as before]: " << d << std::endl;
    
    static_assert(sizeof(uint64_t) == sizeof(newType), "should not be taken for granted ...");
    newType eNotConst;
    std::memcpy(&eNotConst, &randomUintNumber, sizeof(uint64_t));
    const newType e = eNotConst;
    std::cout << "\tstd::memcpy(&eNotConst, &randomUintNumber, sizeof(uint64_t)); [definately reinterprets bit pattern]: " << e << std::endl;
    
    
    return 0;
}

output

INT64_MIN: -9223372036854775808
UINT64_MAX: 18446744073709551615
INT64_MAX: 9223372036854775807

randomUintNumber: 9223372036854785807

as "const int64_t randomIntNumber = reinterpret_cast<int64_t> (randomUintNumber);" does not work, here are some options ...

BAD [undefined behavior!]:
    reinterpret_cast<int64_t&> (randomUintNumber): -9223372036854765809
    reinterpret_cast<int64_t&&> (randomUintNumber): -9223372036854765809

GOOD: 
    (int64_t) randomUintNumber [static cast, sometimes reinterprets bit pattern]: -9223372036854765809
    static_cast<int64_t>(randomUintNumber) [the same as before]: -9223372036854765809
    std::memcpy(&eNotConst, &randomUintNumber, sizeof(uint64_t)); [definately reinterprets bit pattern]: -9223372036854765809
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top