Click here to Skip to main content
15,886,067 members
Articles / Programming Languages / C++

Recursion Primer using C++: Part 3

Rate me:
Please Sign up or sign in to vote.
4.80/5 (12 votes)
6 May 2011CPOL16 min read 34.5K   332   27  
In this article, we try to combine compile time/runtime and structure/generative with five different types of recursion.
// Compile time Generative Nested Recursion

#include <iostream>

template <int m, int n> 
struct Ackermann 
{ 
	// nested recursive call 
	enum { value = Ackermann<m-1, Ackermann<m, n-1>::value>::value }; 
}; 

template <int m> struct Ackermann<m, 0> 
{ 
	// linear recursive call 
	enum { value = Ackermann<m-1, 1>::value }; 
}; 

// termination condition 
template <int n> 
struct Ackermann<0, n> 
{ 
	enum { value = n + 1 }; 
};

int main()
{
	std::cout << Ackermann<2, 3>::value << std::endl; 
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Team Leader American Institute for Research
United States United States
Working as a Team leader in American Institute for Research

Comments and Discussions