How to reverse the order of arguments of a variadic template function?(如何反转可变参数模板函数的参数顺序?)
问题描述
我有一个带有 varargs 模板参数的模板函数,就像这样
I have a template function with varargs template arguments, like this
template<typename Args...>
void ascendingPrint(Args... args) { /* ... */ }
我想写
template<typename Args...>
void descendingPrint(Args... args) {
/* implementation using ascendingPrint()? */
}
我如何反转参数包 args
的顺序,然后再传递它,即在伪代码中:
How do I reverse the order of the parameter-pack args
before passing it along, i.e. in pseudo-code:
template<typename Args...>
void descendingPrint(Args... args) {
ascendingPrint( reverse(args) );
}
推荐答案
这里是一个递归的特殊revert<>
实现:
Here is a recursive implementation of a specialized revert<>
:
// forward decl
template<class ...Tn>
struct revert;
// recursion anchor
template<>
struct revert<>
{
template<class ...Un>
static void apply(Un const&... un)
{
ascendingPrint(un...);
}
};
// recursion
template<class T, class ...Tn>
struct revert<T, Tn...>
{
template<class ...Un>
static void apply(T const& t, Tn const&... tn, Un const&... un)
{
// bubble 1st parameter backwards
revert<Tn...>::apply(tn..., t, un...);
}
};
// using recursive function
template<class A, class ...An>
void descendingPrint(A const& a, An const&... an)
{
revert<An...>::apply(an..., a);
}
它适用于 gcc-4.6/7/8 和 clang 并且可能符合标准——唯一困难的部分是调用revert
.
It works with gcc-4.6/7/8 and clang and is probably standard compliant -- the only difficult part being the call of revert<Tn...>::apply(tn..., t, un...)
.
虽然它有缺点(就像递归经常有的那样),它会生成目标函数的大量模板实例(代码膨胀)并且不使用完美转发,这可能是一个问题(但也许可以改进使用它).
It has drawbacks though (as recursion often has), that it generates a lot of template-instantiations of the target function (code bloat) and does not use perfect forwarding, which may be an issue (but maybe could be improved to use it).
这篇关于如何反转可变参数模板函数的参数顺序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何反转可变参数模板函数的参数顺序?


- 一起使用 MPI 和 OpenCV 时出现分段错误 2022-01-01
- 近似搜索的工作原理 2021-01-01
- 使用/clr 时出现 LNK2022 错误 2022-01-01
- Stroustrup 的 Simple_window.h 2022-01-01
- 从python回调到c++的选项 2022-11-16
- 静态初始化顺序失败 2022-01-01
- STL 中有 dereference_iterator 吗? 2022-01-01
- 与 int by int 相比,为什么执行 float by float 矩阵乘法更快? 2021-01-01
- 如何对自定义类的向量使用std::find()? 2022-11-07
- C++ 协变模板 2021-01-01