for loop elimination

前端 未结 1 733
-上瘾入骨i
-上瘾入骨i 2021-01-14 05:10

I\'d like to use the indices trick to eliminate for loops in my C++11 program (similar to forced -funroll-loops).

Here\'s an example:

相关标签:
1条回答
  • 2021-01-14 05:53

    Try this ( http://liveworkspace.org/code/e65a81d0d3e9b17692713fd3e9d681f5 ):

    template<unsigned...> struct indices {};
    
    template<unsigned S, unsigned E, unsigned... Is> struct indices_gen
      : indices_gen<S, E-1, E-1, Is...>
    {};
    
    template<unsigned S, unsigned... Is> struct indices_gen<S, S, Is...> : indices<Is...>
    {};
    

    Working example: http://liveworkspace.org/code/3d0ba21cc637a61c3e63d2db002f87af

    Edit : Check Xeo's comment. Above example don't check if S < E, so it can go bad if you make a mistake. This (Xeo's code) will return a compiler error in such case: http://liveworkspace.org/code/81205e13334e89537bdc0b79b3ba56fc

    0 讨论(0)
提交回复
热议问题