可变参数模板无法直接使用 _1, _2, ...。您需要改用扩展宏。
但是,您可以将这些占位符包装在模板工厂中,以使用模板参数 1 获取 _1,为 2 获取 _2 等...
诸如 gcc / msvc 之类的实现已经将占位符定义为模板结构(分别为 std::_Placeholder 和 std::_Ph),因此您可以通过这种方式定义您的工厂:
struct ph_factory {
template<size_t holder>
static std::_Placeholder<holder> make_ph() {
return std::_Placeholder<holder>();
}
};
这样定义后,您可以使用所需的所有占位符展开参数包:
struct tester {
template<size_t ... holders>
void test(int val) {
auto callable = std::bind(&tester::call, this, val, ph_factory::make_ph<holders>()...);
callable('a', 42, 'c');
}
void call(int v1, char c1, int v2, char c2) {
cout << "calling :" << v1 << " " << c1 << " " << v2 << " " << c2 << endl;
}
};
所以下面的代码会输出“calling:10 c 42 a”
int main() {
tester t;
t.test<3,2,1>(10);
}
使用 make_indice 之类的技巧将使您有可能实现最初的目标。