使用Variadic模板解压缩参数列表时获取参数索引
Obtain Argument Index while Unpacking Argument List with Variadic Templates
在拆包和转换参数列表时,我需要获得参数的索引。以下问题有什么解决方案吗:
#include <iostream>
#include <vector>
#include <string>
using namespace std;
void test(int a, std::string b, bool c)
{
cout << a << "," << b << "," << c << endl ;
}
template <typename... ARG>
static void call_test(const vector<void*> ¶ms)
{
test(*static_cast<ARG*>(params[ indexOf(ARG) ])...);
}
int main(int argc, char **argv)
{
int a = 1;
string b = "string";
bool c = false;
vector<void*> v(3);
v[0] = &a;
v[1] = &b;
v[2] = &c;
call_test<int,string,bool>(v);
}
我就是这么做的。首先,你需要一些机器来创建整数的编译时间序列:
using namespace std;
//===========================================================================
// META-FUNCTIONS FOR CREATING INDEX LISTS
// The structure that encapsulates index lists
template <size_t... Is>
struct index_list
{
};
// Collects internal details for generating index ranges [MIN, MAX)
namespace detail
{
// Declare primary template for index range builder
template <size_t MIN, size_t N, size_t... Is>
struct range_builder;
// Base step
template <size_t MIN, size_t... Is>
struct range_builder<MIN, MIN, Is...>
{
typedef index_list<Is...> type;
};
// Induction step
template <size_t MIN, size_t N, size_t... Is>
struct range_builder : public range_builder<MIN, N - 1, N - 1, Is...>
{
};
}
// Meta-function that returns a [MIN, MAX) index range
template<size_t MIN, size_t MAX>
using index_range = typename detail::range_builder<MIN, MAX>::type;
//===========================================================================
然后,您可以使用该机制来实现函数调用,利用参数包扩展的功能:
#include <iostream>
#include <vector>
#include <string>
void test(int a, std::string b, bool c)
{
cout << a << "," << b << "," << c << endl ;
}
namespace detail
{
// This is the function that does the real work.
template<typename... Ts, size_t... Is>
void call_test(const vector<void*>& params, index_list<Is...>)
{
test((*static_cast<Ts*>(params[Is]))...);
}
}
// This function just creates the compile-time integer sequence and
// forwards to another function that performs the real work.
// In other words, this is a proxy that hides the complexity of the
// machinery from the client.
template <typename... ARG>
void call_test(const vector<void*>& params)
{
detail::call_test<ARG...>(params, index_range<0, sizeof...(ARG)>());
}
int main(int argc, char **argv)
{
int a = 1;
string b = "string";
bool c = false;
vector<void*> v(3);
v[0] = &a;
v[1] = &b;
v[2] = &c;
call_test<int,string,bool>(v);
}
最简单的解决方案肯定是不使用void*
的vector
;直接使用值即可:
template<typename...Arg>
void call_test(Arg const&...arg) {
test(arg...);
}
(或者这个主题的变体。)但我想你想做的不是调用测试。
如果你真的想要索引,可以使用这里所说的"索引技巧"。你应该能够搜索它。
相关文章:
- 概念中的cv限定符需要表达式参数列表
- 在没有参数列表的情况下使用模板名称"Event"无效,模板问题
- 错误 没有与参数列表匹配的重载函数"getline"实例
- std::vector 没有重载函数的实例与参数列表匹配
- 模板参数列表中的 false 在模板初始化期间计算为什么?
- C++模板/别名 - 模板参数列表中参数 1 处的类型/值不匹配
- 构造函数/函数声明参数列表中的统一初始化
- 模板化检查是否存在带有参数列表的类成员函数?
- 如何将类成员方法的参数列表自动填充写入可变参数?
- 带有整数的变量参数列表
- 转发变量参数列表以模拟 std::thread
- 错误:"模板<类_Tp,类_Dp>类 std::unique_ptr"的模板参数列表中参数 1 的类型/值不匹配
- C++-将具有引用的长参数列表重构为结构
- 缺少别名模板C++参数列表
- C++如果两个模板函数都与参数列表匹配,将调用哪个模板
- "extern"声明以及带有和不带参数列表的类模板实例的后续定义
- 使用显式模板参数列表和 [temp.arg.explicit]/3 的函数调用的演绎失败
- 没有函数模板的实例与我不知道为什么的参数列表匹配
- 我可以使用宏自动构建参数列表吗?
- 类成员函数参数列表是否可以依赖于模板参数?