- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我有一个带有许多模板参数的模板函数,它看起来像这样:
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/
我正在用 C# 编写动态语言的解释器,并将原始函数实现为具有虚拟 Apply 方法的抽象类 Primitive,其中每个实际原始函数都是重写 Apply 的子类。 (另一种方法是只拥有类 Primit
我正在用 C# 编写动态语言的解释器,并将原始函数实现为具有虚拟 Apply 方法的抽象类 Primitive,其中每个实际原始函数都是重写 Apply 的子类。 (另一种方法是只拥有类 Primit
我是 Dapper 的新手我正在尝试了解它实际上是如何映射事物的。我有以下数据库结构: calendar | Id | Name | meeting_room | Id | Calendar_id
抱歉问题标题很糟糕。有没有办法在一行中做到这一点: Button button = (Button)Gridview.Cells[0].FindControl("controlname"); butt
在 Java 中在声明点和使用点声明列表/数组文字的tersest方法是什么? 作为次要问题,我更喜欢一种不会导致编译时警告或要求抑制警告的方法。 注意:就我个人而言,这是针对Java 8ish on
什么是现代、简洁、快速的方法来测试节点是否有任何与给定选择器匹配的子节点? “简洁”是指类似于 jQuery 或函数式风格,例如避免循环。我知道本地选择器越来越多地使用这种类型的东西,但没有跟上发展的
getFirstNotNullResult 执行函数列表,直到其中一个函数返回非空值。 如何更优雅/简洁地实现 getNotNullFirstResult? object A { def main
根据 stackoverflow 上某人的推荐,我使用了 jquery succint https://github.com/micjamking/Succinct截断我在 php 网站上的帖子。 它
我是一名优秀的程序员,十分优秀!