gpt4 book ai didi

c++ - 如何简洁地初始化具有多个参数的模板函数?

转载 作者:太空狗 更新时间:2023-10-29 21:13:04 25 4
gpt4 key购买 nike

我有一个带有许多模板参数的模板函数,它看起来像这样:

template <int a, int b, int c>
void foo(){
for(int i = 0 ; i < a ; i ++)
for(int j = 0 ; j < b ; j ++)
for(int k = 0 ; k < c ; k ++)
//do something
}

在这种情况下,使用模板参数 (a b c) 以便编译器可以展开这些循环。但在实践中,这些参数可能会取很多值。比如说,如果 a/b/c 中的每一个都可以从 [1,2,3,4] 中取值,那么总共有 64 个模板函数需要初始化。所以,你的代码会是这样的:

if(a == 1 && b == 1 && c == 1) foo<1,1,1>();
else if(a == 1 && b == 1 &&c == 2) foo<1,1,2>();
//......
else foo<4,4,4>();

那是很多可怕的编码。那么,您有什么简洁的方法可以做到这一点吗?

最佳答案

您可以通过使用嵌套模板函数一次确定每个模板参数来消除大量重复。这是一种 hack,如果范围相当大,那么您可能希望使用代码生成来创建这些包装器。这不是非常理想,但是编写三个函数,每个函数包含四个案例,肯定比编写 43 个条件要好。

void foo0(int a, int b, int c)
{
switch (a) {
case 1: foo1<1>(b, c); break;
case 2: foo1<2>(b, c); break;
case 3: foo1<3>(b, c); break;
case 4: foo1<4>(b, c); break;
}
}

template <int a>
void foo1(int b, int c)
{
switch (b) {
case 1: foo2<a, 1>(c); break;
case 2: foo2<a, 2>(c); break;
case 3: foo2<a, 3>(c); break;
case 4: foo2<a, 4>(c); break;
}
}

template <int a, int b>
void foo2(int c)
{
switch (c) {
case 1: foo<a, b, 1>(); break;
case 2: foo<a, b, 2>(); break;
case 3: foo<a, b, 3>(); break;
case 4: foo<a, b, 4>(); break;
}
}

您可以将整个任务委托(delegate)给编译器,但代码比这一段要长得多,所以如果您的范围大于 [1, 5),您应该只沿着那条路走下去。这是 an example这种方法。它冗长且笨拙(并且可能会被更聪明的人简化)但它将完全在编译时生成决策树。请注意,这棵树不是最优的,但这在您的情况下可能重要也可能不重要。

#include <iostream>

template <int a, int b, int c>
void foo()
{
std::cout << "foo<" << a << "," << b << "," << c << ">()\n";
}

// Class wrapper so that we can generically apply this function.
template <int a, int b, int c>
struct foo_wrapper
{
void operator()() { foo<a, b, c>(); }
};

template <template <int, int, int> class fn, int min, int max, int a, int b, int i>
struct caller_p3
{
static void call(int c) {
if (c == i) {
fn<a, b, i>()();
} else {
caller_p3<fn, min, max, a, b, i + 1>::call(c);
}
}
};

template <template <int, int, int> class fn, int min, int max, int a, int b>
struct caller_p3<fn, min, max, a, b, max>
{
static void call(int c) {
if (c == max) {
fn<a, b, max>()();
} else {
// out of range, throw?
}
}
};

template <template <int, int, int> class fn, int min, int max, int a, int i>
struct caller_p2
{
static void call(int b, int c) {
if (b == i) {
caller_p3<fn, min, max, a, i, min>::call(c);
} else {
caller_p2<fn, min, max, a, i + 1>::call(b, c);
}
}
};

template <template <int, int, int> class fn, int min, int max, int a>
struct caller_p2<fn, min, max, a, max>
{
static void call(int b, int c) {
if (b == max) {
caller_p3<fn, min, max, a, max, min>::call(c);
} else {
// out of range, throw?
}
}
};

template <template <int, int, int> class fn, int min, int max, int i>
struct caller_p1
{
static void call(int a, int b, int c) {
if (a == i) {
caller_p2<fn, min, max, i, min>::call(b, c);
} else {
caller_p1<fn, min, max, i + 1>::call(a, b, c);
}
}
};

template <template <int, int, int> class fn, int min, int max>
struct caller_p1<fn, min, max, max>
{
static void call(int a, int b, int c) {
if (a == max) {
caller_p2<fn, min, max, max, min>::call(b, c);
} else {
// out of range, throw?
}
}
};

// Generic caller.
template <template <int, int, int> class fn, int min, int max>
struct caller
{
void operator()(int a, int b, int c) {
caller_p1<fn, min, max, min>::call(a, b, c);
}
};

int main() {
caller<foo_wrapper, 0, 5>()(1, 2, 3);
caller<foo_wrapper, 0, 5>()(0, 0, 5);
caller<foo_wrapper, 0, 5>()(5, 1, 0);
}

Here is a C++11 implementation使用可变模板允许任意数量的 foo() 模板参数:

#include <iostream>

template <int a, int b, int c>
void foo()
{
std::cout << "foo<" << a << "," << b << "," << c << ">()\n";
}

// Class wrapper so that we can generically apply this function.
template <int a, int b, int c>
struct foo_wrapper
{
void operator()() { foo<a, b, c>(); }
};

// Caller implementation.
template <typename T, template <T...> class fn, T min, T max, T i, T... parms>
struct caller_impl
{
template <typename... Tail>
static void call(T head, Tail... tail)
{
if (head == i) {
caller_impl<T, fn, min, max, min, parms..., i>::call(tail...);
} else {
caller_impl<T, fn, min, max, i + 1, parms...>::call(head, tail...);
}
}

static void call()
{
fn<parms...>()();
}
};

// Specialization for i==max
template <typename T, template <T...> class fn, T min, T max, T... parms>
struct caller_impl<T, fn, min, max, max, parms...>
{
template <typename... Tail>
static void call(T head, Tail... tail)
{
if (head == max) {
caller_impl<T, fn, min, max, min, parms..., max>::call(tail...);
} else {
// Out of range, throw?
}
}

static void call()
{
fn<parms...>()();
}
};

// Helper to kick off the call.
template <typename T, template <T...> class fn, T min, T max, typename... parms>
void caller(parms... p)
{
caller_impl<T, fn, min, max, min>::call(p...);
}

int main() {
caller<int, foo_wrapper, 0, 5>(1, 2, 3);
caller<int, foo_wrapper, 0, 5>(0, 0, 5);
caller<int, foo_wrapper, 0, 5>(5, 1, 0);
}

关于c++ - 如何简洁地初始化具有多个参数的模板函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45626437/

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