How compile time recursion works?

后端 未结 5 2166
没有蜡笔的小新
没有蜡笔的小新 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条回答
  •  梦毁少年i
    2021-02-04 02:41

    Simple enough, each template instanciation create a new function with the changed parameter. Like if you defined: f1_1000(), f1_999() and so on.

    Each function call the function with 1 less in it's name. As there is a different template, not recursive, to define f1_1() we also have a stop case.

提交回复
热议问题