Jan 18, 2014

[C/C++][NOTE] Tail recursive call

Reference:
Tail Call
Tail recursion in C++
Tail Recursion in C++ with multiple recursive function calls
Does C++11 does optimise away tail recursive calls in lambdas?
Tackling C++ Tail Calls
https://llvm.org/docs/CodeGenerator.html#tail-call-optimization


#include <iostream>                                                                       
                                                                                          
template<typename T, int MeaningOfLife>                                                   
struct Fun                                                                                
{                                                                                         
   Fun()                                                                                  
   {                                                                                      
      std::move(*this).how(MeaningOfLife);                                                
   }                                                                                      
                                                                                          
   T how(int i)&&                                                                         
   {                                                                                      
      using namespace std;                                                                
      cout << "how ";                                                                     
      return std::move(*this).areyou(i);                                                  
   }                                                                                      
                                                                                          
   T areyou(int i)&&                                                                      
   {                                                                                      
      using namespace std;                                                                
      cout << "are you?" << endl;                                                         
      return std::move(*this).how(i);                                                     
   }                                                                                      
};                                                                                        
                                                                                          
                                                                                          
int main()                                                                                
{                                                                                         
   Fun<void, 42>();                                                                       
} 

Tail recursive Use with -O3 and without optimize to see how compiler optimizing the Tail recursive call. No more stack-overflow for -O3

* Expand in the callee, not the caller.
* Tail call could be translated to a local loop structure

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.