How compile time recursion works?

后端 未结 5 2165
没有蜡笔的小新
没有蜡笔的小新 2021-02-04 01:44

I found a code here Printing 1 to 1000 without loop or conditionals

Can someone please explain how compile time recursion works, couldn\'t find it in google



        
5条回答
  •  夕颜
    夕颜 (楼主)
    2021-02-04 02:29

    It works conceptually almost the same way as runtime recursion. f1<1000> calls f1<999> and then prints out 1000. f1<999> calls f1<998> and then prints out 999, etc. Once it gets to 1 the template specialization acts as the base case to abort the recursion.

提交回复
热议问题