###### tags: `C++` # Meta Programming to unroll a function > The code is copied from the [Lightning Talk: C++20 - A New Way of Meta-Programming? - Kris Jusiak - CppCon 2022](https://www.youtube.com/watch?v=zRYlQGMdISI&t=271s) ## What I want: ```cpp= void print() { std::cout<<"hello world\n"; } int main() { unroll<5>(print); } /* expected output hello world hello world hello world hello world hello world */ ``` ## template design: ### The method in the video before c++20 ```cpp= template<auto N> constexpr auto unroll = [](auto expr) { [expr]<auto ...Is>(std::index_sequence<Is...>) { ((expr(), void(Is)), ...); }(std::make_index_sequence<N>{}); }; ``` ### The method should be after c++20 ```cpp= template<auto N> constexpr auto unroll = [](auto expr) { auto [... Is] = std::make_index_sequence<N>{}; ((expr(), void(Is)), ...); } ``` ### The method without lambda ```cpp= template <typename Callable, auto... Is> constexpr void deep_unroll(Callable expr, std::index_sequence<Is...>) { ((expr(), void(Is)), ...); } template <auto N, typename Callable> constexpr void unroll2(Callable expr) { deep_unroll(expr, std::make_index_sequence<N>{}); } ``` ### demo code [code](https://godbolt.org/z/oMhrKc1Md)