好的,我想我知道了,看看:
#include <functional>
#include <iostream>
template<typename T>
T recrusiveSearchHelper(int which,int current, T last)
{
return last;
};
template<typename T, typename... Args>
T recrusiveSearchHelper(int which,int current,T first,Args... args)
{
if (which == current)
{
return first;
}
recrusiveSearchHelper(which,current + 1,args...);
}
template <typename T,typename... Args>
T recrusiveSearch(int which, T first, Args... args)
{
return recrusiveSearchHelper(which,0,first,args...);
}
struct Seperator
{
};
void foo2(double a,double b,double d)
{
std::cout << "Foo two: " << a << " : " << b << " : " << d << std::endl;
};
void foo1(int a,int b)
{
std::cout << "Foo One: " << a << " : "<< b << std::endl;
};
void zipper(int& index,std::function<void(int)>fn, Seperator s)
{
index++;
fn(index);
}
template <typename T>
void zipper(int& index, std::function<void(int)>fn, T data)
{
index++;
};
template <typename T,typename... Args>
void zipper(int& index, std::function<void(int)> fn,T first, Args... args)
{
index++;
zipper(index,fn,args...);
};
template <typename... Args>
void zipper(int& index, std::function<void(int)> fn,Seperator first, Args... args)
{
index++;
fn(index);
zipper(index,fn,args...);
};
template <typename T,typename... Args>
void useWithSeperator(T first,Args... args)
{
int index = 0;
int current = 0;
std::function <void(int)> fn = [¤t,first,args...](int where)
{
if (where - current == 3)
{
foo1(recrusiveSearch(where-3,first,args...),recrusiveSearch(where-2,first,args...));
}else if (where - current == 4)
{
foo2(recrusiveSearch(where-4,first,args...),recrusiveSearch(where-3,first,args...),recrusiveSearch(where-2,first,args...));
}
current = where;
};
zipper(index,fn,first);
zipper(index,fn,args...);
};
int main(int argc, char **argv)
{
useWithSeperator(1,2,3,Seperator(),4,5,Seperator(),1,1,2,3,4,Seperator(),1,2,5,Seperator());
}
它有点混乱,但是它的作用是通过一个辅助变量模板运行,该模板在每次迭代时缩小我们的搜索范围,直到元素在其递归循环中首先成为我们所需位置的元素。剩下的很简单,我们需要检索数据并跟踪我们的周期有多长。
我会尽快为这个答案添加更多细节。