3

我正在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];

我的直觉告诉我,所有这些东西都实现了某种跨平台可移植性。我是对的,还是还有更多?

4

3 回答 3

5

看起来他们正在重新发明stdint.h(我相信在某些/许多版本的 MS 编译器中不支持),为基于对sizeof. 请注意,接受的第四个模板参数sizeof(char)完全无用,sizeof(char)始终为 1。

于 2012-10-12T16:59:29.200 回答
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);
}
于 2012-10-12T17:54:10.873 回答
1

这段代码计算出不同基本标量类型的大小,并根据它定义了一些类型。

所以是的,它用于跨平台兼容性。但是,为什么以这种方式使用它超出了我的理解。

于 2012-10-12T16:57:34.030 回答