強制的なアンループなんて、手垢のついたネタだし、boost のどこかにありそうなもんだけど、探すより書くほうが早そうだったから。

  • 修正: std::size_t Size の特殊化の際に、マクロ BOOST_PP_INC(n) ではなく n を使っていた。

#ifndef STATIC_FOR_H
#define STATIC_FOR_H

#include <cstddef>
#include <boost/preprocessor/arithmetic/inc.hpp>
#include <boost/preprocessor/config/limits.hpp>
#include <boost/preprocessor/repetition/repeat.hpp>

#define PP_FUNC(z, n, _) func(n);

#define PP_CLASS(z, n, _) \
template <> \
class Static_For<BOOST_PP_INC(n)> { \
public: \
template <typename Function> \
static Function static_for(Function func) { \
BOOST_PP_REPEAT_##z(BOOST_PP_INC(n), PP_FUNC, _) \
return func; \
} \
};

///////////////////////////////////////////////////////////////////////////

template <std::size_t Size>
class Static_For {
public:
template <typename Function>
static Function static_for(Function func) {
for (std::size_t i = 0; i < Size; ++i) {
func(i);
}
return func;
}
};

BOOST_PP_REPEAT(BOOST_PP_LIMIT_REPEAT, PP_CLASS, _)

///////////////////////////////////////////////////////////////////////////

#undef PP_FUNC
#undef PP_CLASS

#endif // STATIC_FOR_H