#include <iostream>
// 通过递归实现
template <int Beg, int End>
struct static_for
{
template <typename Fn>
void operator()(const Fn& fn) const
{
if (Beg < End)
{
fn(Beg);
static_for<Beg+1, End>()(fn); // 最后一次,2个数相等了,会调用下面的 static_for<N, N>
}
}
};
template <int N>
struct static_for<N, N>
{
template <typename Fn>
void operator()(Fn const& fn) const
{
std::cout << "static_for<N, N>" << std::endl;
}
};
int main() {
static_for<0, 5>()([&](int i){
std::cout << i << std::endl;
});
return 0;
}
原创文章,作者:ItWorker,如若转载,请注明出处:https://blog.ytso.com/289375.html