template conversion
I have two classes int_t
, uint_t
as signed type and unsigned type:
template <typename lo_t> struct uint_t;
template <typename hi_t, typename lo_t>
struct int_t
{
lo_t lo;
hi_t hi;
int_t() : hi(0), lo(0) {}
int_t(int value) : lo(value), hi(value<0? -1: 0) {}
int_t(unsigned value) : lo(value), hi(0) {}
int_t(const uint_t<lo_t>&);
//template<typename ty_a, typename ty_b> int_t(const int_t<ty_a, ty_b>&);
};
template <typename hi_lo>
struct uint_t
{
hi_lo lo, hi;
uint_t() : lo(0), hi(0) {}
uint_t(int value) : lo(value), hi(value<0? -1: 0) {}
uint_t(unsigned value) : lo(value), hi(0) {}
template<typename hi_t>
uint_t(const int_t<hi_t, hi_lo>& value) : hi(value.hi), lo(value.lo) {}
};
template <typename hi_t, typename lo_t>
int_t<hi_t, lo_t>::int_t(const uint_t<lo_t>& value) : hi(value.hi), lo(value.lo)
{}
Because I want them to work just like built-in types I defined conversion operator from one to another so I can write code like next and still works:
typedef int_t<int, unsigned> int64;
typedef uint_t<unsigned> uint64;
int64 a = 1000;
uint64 b = a;
uint64 x = 512;
int64 y = x;
Now the only problem left is to convert from higher or lower precision int_t
type to the other, so I declared the commented constructor to do so but I don't know what to write in it?
Here is an example that i used to test the result of that co开发者_如何转开发nstructor:
typedef int_t<int, unsigned> int64;
typedef uint_t<unsigned> uint64;
typedef int_t<int64, uint64> int128;
typedef uint_t<uint64> uint128;
int64 a = 1024;
int128 b = a;
int128 x = 100;
int64 y = x;
You have to define what you want them to do. For unsigned, increasing size is easy, you just set the high bits to 0 and copy the low bits from the operand. For signed you may wish to sign-extend the operand.
For shrinking you also have to decide what you want to do. Do you want to throw if the value doesn't fit? Most likely you just want to throw away all the high order bits and store it in the available space.
I figured the answer for Little and Big Endians:
template<typename ty_a, typename ty_b>
int_t(const int_t<ty_a, ty_b>& value)
{
*this = value < 0? -1: 0;
#ifdef BIG_ENDIAN
if (sizeof(*this) < sizeof(value))
*this = *((int_t*)&value.lo + (sizeof(value.lo)/sizeof(*this) - 1));
else
*((int_t<ty_a, ty_b>*)&hi + sizeof(*this)/sizeof(value) - 1) = value;
#else
if (sizeof(*this) < sizeof(value))
*this = *(int_t*)&value;
else
*(int_t<ty_a, ty_b>*)&lo = value;
#endif
}
remember the answer required operator==
and operator<
to be defined for int_t
精彩评论