c++静态for循环
作者:互联网
#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;
}
标签:const,cout,静态,c++,循环,static,template,operator,fn 来源: https://www.cnblogs.com/icefoxhz/p/16691960.html