#include <iostream>

// A union_cast can be used to do casting between types of different sizes.
// A typical, usage is to put and retrieve a 32 bits integer into a pointer.
// Such operation implies, on a 64 bits machine, to cast a 64 bits value into
// a 32 bits value. This is not possible using a reinterpret_cast expression as
// stated in section of the IEC-14882-2003 C++ standard: a pointer can
// be explicitly converted to any integral type large enough to hold it.
// It seems to be a common C programming idiom to use unions to perform what
// C++ calls a reinterpret_cast, thus the name union_cast.
template <class Dest, class Source>
inline Dest union_cast(Source source) {

    union {
        Source source;
        Dest dest;
    } converter;

    converter.source = source;
    return converter.dest;

int main() {

    using namespace std;

    // Declare a 32 unsigned integer.
    unsigned int a = 0x11112222;

    // Store its value into a, potentially, 64 bits pointer.
    void* b = union_cast<void*>(a);

    // Retrieve the original 32 bits unsigned
    // integer from the void pointer.
    unsigned int c = union_cast<unsigned int>(b);

    cout << hex << a << endl;
    cout << hex << b << endl;
    cout << hex << c << endl;

    return 0;