quote:Original post by Anonymous Poster
What do you mean? I think those typedefs would be very handly in C++ too and have no idea of how templates would help to fix the need for them.
Check out my post in this thread for an example..
quote:Original post by Anonymous Poster
What do you mean? I think those typedefs would be very handly in C++ too and have no idea of how templates would help to fix the need for them.
template <typename T,size_t N> struct integer;
template <> struct integer<signed,CHAR_BIT*sizeof(signed char)> { typedef signed char type; };
template <> struct integer<signed,CHAR_BIT*sizeof(signed short)> { typedef signed short type; };
template <> struct integer<signed,CHAR_BIT*sizeof(signed int)> { typedef signed int type; };
template <> struct integer<signed,CHAR_BIT*sizeof(signed long)> { typedef signed long type; };
/* ... */
template <> struct integer<unsigned,CHAR_BIT*sizeof(unsigned long)> { typedef unsigned long type; };
typedef integer<signed,16>::type int16_t;
climits
and the second through a simple metaprogram. A more elegant solution should be possible using typelists and a slightly less simple metaprogram. template<int I>struct AtLeast { typedef typename AtLeast<I+1>::Signed Signed; typedef typename AtLeast<I+1>::Unsigned Unsigned; };template<>struct AtLeast<8> { typedef i8 Signed; typedef u8 Unsigned; };template<>struct AtLeast<16> { typedef i8 Signed; typedef u8 Unsigned; };template<>struct AtLeast<32> { typedef i8 Signed; typedef u8 Unsigned; };template<>struct AtLeast<64> { typedef i8 Signed; typedef u8 Unsigned; }; template<>struct AtLeast<65> { //TODO //int NoInOver64[0]; }; template<int I>struct AtMost { typedef typename AtMost<I-1>::Signed Signed; typedef typename AtMost<I-1>::Unsigned Unsigned; };template<>struct AtMost<0> { typedef void Signed; typedef void Unsigned; };template<>struct AtMost<8> { typedef i8 Signed; typedef u8 Unsigned; };template<>struct AtMost<16> { typedef i8 Signed; typedef u8 Unsigned; };template<>struct AtMost<32> { typedef i8 Signed; typedef u8 Unsigned; };template<>struct AtMost<64> { typedef i8 Signed; typedef u8 Unsigned; }; template<>struct AtMost<65> { //TODO };template<unsigned int I>struct Log2 { private: typedef Log2<I/2> Log2Calc; public: static const Upper = 1+Log2Calc::Upper; static const Lower = 1+Log2Calc::Lower; };template<>struct Log2<1> { static const Upper = 1; static const Lower = 0; };template<>struct Log2<0> { //TODO };
quote:Original post by Magmai Kai Holmlor
static const BufferSize = sizeof(buffer);
typedef typename AtLeast< Log2::Upper >::Unsigned IndexInt;
char buffer[1..255]; -> IndexInt ~ char
char buffer[256..65535]; -> IndexInt ~ short
char buffer[65536..4294967295]; -> IndexInt ~ int
template<int I>struct AtLeast { typedef typename AtLeast<I+1>::Signed Signed; typedef typename AtLeast<I+1>::Unsigned Unsigned; };template<>struct AtLeast<8> { typedef i8 Signed; typedef u8 Unsigned; };template<>struct AtLeast<16> { typedef i8 Signed; typedef u8 Unsigned; };template<>struct AtLeast<32> { typedef i8 Signed; typedef u8 Unsigned; };template<>struct AtLeast<64> { typedef i8 Signed; typedef u8 Unsigned; }; template<>struct AtLeast<65> { //TODO //int NoInOver64[0]; }; template<int I>struct AtMost { typedef typename AtMost<I-1>::Signed Signed; typedef typename AtMost<I-1>::Unsigned Unsigned; };template<>struct AtMost<0> { typedef void Signed; typedef void Unsigned; };template<>struct AtMost<8> { typedef i8 Signed; typedef u8 Unsigned; };template<>struct AtMost<16> { typedef i8 Signed; typedef u8 Unsigned; };template<>struct AtMost<32> { typedef i8 Signed; typedef u8 Unsigned; };template<>struct AtMost<64> { typedef i8 Signed; typedef u8 Unsigned; }; template<>struct AtMost<65> { //TODO };template<unsigned int I>struct Log2 { private: typedef Log2<I/2> Log2Calc; public: static const Upper = 1+Log2Calc::Upper; static const Lower = 1+Log2Calc::Lower; };template<>struct Log2<1> { static const Upper = 1; static const Lower = 0; };template<>struct Log2<0> { //TODO };