我正在http://sourceforge.net/projects/dtmf/学习DTMF代码。我遇到了一些我无法理解的C ++代码:
template<int, int, int, int> class Types;
template <> class Types<5, 4, 2, 1>
{
public:
typedef long int Int40;
typedef unsigned long int Uint40;
typedef int Int32;
typedef unsigned int Uint32;
typedef short int Int16;
typedef unsigned short int Uint16;
typedef char Int8;
typedef unsigned char Uint8;
};
template <> class Types<8, 4, 2, 1>
{
public:
typedef long int Int64;
typedef unsigned long int Uint64;
typedef int Int32;
typedef unsigned int Uint32;
typedef short int Int16;
typedef unsigned short int Uint16;
typedef char Int8;
typedef unsigned char Uint8;
};
template <> class Types<4, 4, 2, 1>
{
public:
typedef int Int32;
typedef unsigned int Uint32;
typedef short int Int16;
typedef unsigned short int Uint16;
typedef char Int8;
typedef unsigned char Uint8;
};
// For 16bit chars
template <> class Types<2, 1, 1, 1>
{
public:
typedef long int Int32;
typedef unsigned long int Uint32;
typedef short int Int16;
typedef unsigned short int Uint16;
};
typedef Types<sizeof(long int), sizeof(int), sizeof(short int), sizeof(char)>::Int32 INT32;
typedef Types<sizeof(long int), sizeof(int), sizeof(short int), sizeof(char)>::Uint32 UINT32;
typedef Types<sizeof(long int), sizeof(int), sizeof(short int), sizeof(char)>::Int16 INT16;
typedef Types<sizeof(long int), sizeof(int), sizeof(short int), sizeof(char)>::Uint16 UINT16;
从那里开始,它们就像普通的原始类型一样使用:
static const INT16 tempCoeff[8];
我的直觉告诉我,所有这些东西都实现了某种跨平台的可移植性。我是对的还是还有更多呢?
答案 0 :(得分:5)
看起来他们正在重新发明stdint.h
(我相信在一些/许多版本的MS编译器中不支持),它基于对{{{{{{{{ 1}}。请注意,接受sizeof
的第四个模板参数完全无效,因为sizeof(char)
始终为1。
答案 1 :(得分:2)
让我们看看我们是否可以设计一些更合理的方法(要求为平台正确定义CHAR_BIT
):
namespace portable_inttypes
{
template<typename Tchain, typename T, typename Tun, size_t Tsize = sizeof (T) * CHAR_BIT, bool atleast64 = Tsize >= 64>
struct autodef_helper64 : Tchain {};
template<typename Tchain, typename T, typename Tun, size_t Tsize>
struct autodef_helper64<Tchain, T, Tun, Tsize, true> : Tchain
{
typedef T int_least64_t;
typedef Tun uint_least64_t;
};
template<typename Tchain, typename T, typename Tun>
struct autodef_helper64<Tchain, T, Tun, 64, true> : Tchain
{
typedef T int64_t, int_least64_t;
typedef Tun uint64_t, uint_least64_t;
};
template<typename Tchain, typename T, typename Tun, size_t Tsize = sizeof (T) * CHAR_BIT, bool atleast32 = Tsize >= 32>
struct autodef_helper32 : autodef_helper64<Tchain, T, Tun> {};
template<typename Tchain, typename T, typename Tun, size_t Tsize>
struct autodef_helper32<Tchain, T, Tun, Tsize, true> : autodef_helper64<Tchain, T, Tun>
{
typedef T int_least32_t;
typedef Tun uint_least32_t;
};
template<typename Tchain, typename T, typename Tun>
struct autodef_helper32<Tchain, T, Tun, 32, true> : autodef_helper64<Tchain, T, Tun>
{
typedef T int32_t, int_least32_t;
typedef Tun uint32_t, uint_least32_t;
};
template<typename Tchain, typename T, typename Tun, size_t Tsize = sizeof (T) * CHAR_BIT, bool atleast32 = Tsize >= 16>
struct autodef_helper16 : autodef_helper32<Tchain, T, Tun> {};
template<typename Tchain, typename T, typename Tun, size_t Tsize>
struct autodef_helper16<Tchain, T, Tun, Tsize, true> : autodef_helper32<Tchain, T, Tun>
{
typedef T int_least16_t;
typedef Tun uint_least16_t;
};
template<typename Tchain, typename T, typename Tun>
struct autodef_helper16<Tchain, T, Tun, 16, true> : autodef_helper32<Tchain, T, Tun>
{
typedef T int16_t, int_least16_t;
typedef Tun uint16_t, uint_least16_t;
};
template<typename Tchain, typename T, typename Tun, size_t Tsize = sizeof (T) * CHAR_BIT, bool atleast8 = Tsize >= 8>
struct autodef_helper8 : autodef_helper16<Tchain, T, Tun> {};
template<typename Tchain, typename T, typename Tun, size_t Tsize>
struct autodef_helper8<Tchain, T, Tun, Tsize, true> : autodef_helper16<Tchain, T, Tun>
{
typedef T int_least8_t;
typedef Tun uint_least8_t;
};
template<typename Tchain, typename T, typename Tun>
struct autodef_helper8<Tchain, T, Tun, 8, true> : autodef_helper16<Tchain, T, Tun>
{
typedef T int8_t, int_least8_t;
typedef Tun uint8_t, uint_least8_t;
};
struct autodef_base {};
typedef autodef_helper8<autodef_base, long long, unsigned long long> autodef_longlong;
typedef autodef_helper8<autodef_longlong, long, unsigned long> autodef_long;
typedef autodef_helper8<autodef_long, int, unsigned> autodef_int;
typedef autodef_helper8<autodef_int, short, unsigned short> autodef_short;
typedef autodef_helper8<autodef_short, signed char, unsigned char> autodef_char;
}
typedef portable_inttypes::autodef_char inttypes;
int main(void)
{
return sizeof(inttypes::uint32_t);
}
答案 2 :(得分:1)
此代码计算出不同基本标量类型的大小,并根据它定义几种类型。
是的,它用于跨平台兼容性。为什么它以的方式使用,但是我的理解还不够。