gpt4 book ai didi

c++ - 循环类型定义

转载 作者:太空狗 更新时间:2023-10-29 23:54:11 26 4
gpt4 key购买 nike

我有一个无聊的函数要运行,我想遍历它以节省这次时间(我有所有数据),但它需要类型。有没有办法制作一个类型数组,或者有一些时间可以做到这一点? (如果有帮助,我有 3 种类型,并希望针对所有类型运行所有类型的方法)。

fprintf(stdout, "Testing UTF-32...\n");
testUTF<uint32_t, uint32_t>(&testEncs[0], &testEncs[0]);
testUTF<uint32_t, uint16_t>(&testEncs[0], &testEncs[1]);
testUTF<uint32_t, uint8_t> (&testEncs[0], &testEncs[2]);

fprintf(stdout, "Testing UTF-16...\n");
testUTF<uint16_t, uint32_t>(&testEncs[1], &testEncs[0]);
testUTF<uint16_t, uint16_t>(&testEncs[1], &testEncs[1]);
testUTF<uint16_t, uint8_t> (&testEncs[1], &testEncs[2]);

fprintf(stdout, "Testing UTF-8...\n");
testUTF<uint8_t, uint32_t>(&testEncs[2], &testEncs[0]);
testUTF<uint8_t, uint16_t>(&testEncs[2], &testEncs[1]);
testUTF<uint8_t, uint8_t> (&testEncs[2], &testEncs[2]);

最佳答案

template <int I> struct UType;
template <> struct UType<0> { typedef uint32_t Type; };
template <> struct UType<1> { typedef uint16_t Type; };
template <> struct UType<2> { typedef uint8_t Type; };
static const int n_types = 3;

template <int A,int B>
struct Test {
typedef typename UType<A>::Type AType;
typedef typename UType<B>::Type BType;
static void test()
{
testUTF<AType,BType>(&testEncs[A],&testEncs[B]);
Test<A,B+1>::test();
}
};

template <int A>
struct Test<A,n_types> {
static void test() { Test<A+1,0>::test(); }
};

template <>
struct Test<n_types,0> {
static void test() { }
};

void testAll()
{
Test<0,0>::test();
}

关于c++ - 循环类型定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7495423/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com