In one of my previous articles, “compile-time
repeat & noexcept-correctness”, I have
covered the design and implementation of a simple
repeat<n>(f) function that, when invoked, expands to
n calls to f during compilation. E.g.
repeat<4>([]{ std::cout << "hello\n"; });…is roughly equivalent to…
[]{ std::cout << "hello\n"; }();
[]{ std::cout << "hello\n"; }();
[]{ std::cout << "hello\n"; }();
[]{ std::cout << "hello\n"; }();If you squint, this is a very limited form of compile-time iteration. When writing generic code, I’ve often needed similar constructs in order to express the following actions: