Program Template to Fuse Loop
Clash Royale CLAN TAG #URR8PPP Program Template to Fuse Loop How to fuse for loop with template? Method 1 fuses the three loops. Method 2 seems not. (or the compiler has already done it?) gcc god bolt for method 1 gcc god bolt for method 2 code: #include <iostream> #include <type_traits> template <int Depth> inline void loop(int* dim, int* counter, int size){ int d = size - Depth; for( counter[d] = 0; counter[d] < dim[d]; ++counter[d]){ loop<Depth - 1>(dim, counter,size); } } template <> inline void loop<0>(int dim, int counter, int size){ std::cout<<"#"; } int main(void){ int dim = {2,3,2}; int counter = {0,0,0}; //Method 1 for(int i = 0; i < 12; ++i) std::cout <<"#"; /* //Method 2 loop<3>(dim, counter, 3); */ } clang inlines and unrolls all loops with method 2, gcc seems to miss the optimisation opportun...