How compile time recursion works?

后端 未结 5 2173
没有蜡笔的小新
没有蜡笔的小新 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

    This is not guaranteed to be pure compile-time recursion. The compiler will have to instantiate function f1() for all parameters value from 2 to 1000 and they will call each other.

    Then the compiler might see that those calls can be just turned into a sequence of cout << ... statements. Maybe it eliminates calls, maybe not - this is up to the compiler. From the point of C++ this is a chain of function calls and the compiler can do whatever as long as it doesn't alter behavior.

提交回复
热议问题