Typedef

Started by
24 comments, last by tstrimp 20 years, 11 months ago
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..
Advertisement
Yes, they would be handy and I do hope they will get added to the standard. But as long as you only need typedefs for built-in integer types (ie no __int64) they're not strictly necessary in C++.

Here's the basic idea:

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;


That won't compile as is because there's no guarantee that sizeof(int) != sizeof(short) or that a 16 bit integer type exists. The first problem can be solved using the defines in climits and the second through a simple metaprogram. A more elegant solution should be possible using typelists and a slightly less simple metaprogram.

[edited by - spock on May 1, 2003 6:08:54 PM]
Integer Type Selection Templates

static const BufferSize = sizeof(buffer);
typedef typename AtLeast< Log2<BufferSize>::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	};  


- The trade-off between price and quality does not exist in Japan. Rather, the idea that high quality brings on cost reduction is widely accepted.-- Tajima & Matsubara
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	};    





I totally missed that one
daerid@gmail.com
*clap clap*
Clever quoting, daerid. The original post is right above yours yet you had to quote the entire thing?

This topic is closed to new replies.

Advertisement