is c++ Template Metaprogramming a form of functional programming

Viewed 4084

is c++ Template Metaprogramming a form of functional programming? If it is, do some pitfalls like stackoverflow for non-tail recursion relevant for c++ Template Metaprogramming?

For the factorial template example in this question, I guess it is standard functional programming. Or the similarity is only superficial?

#include <iostream>
using namespace std;

template< int n >
struct factorial { enum { ret = factorial< n - 1 >::ret * n }; };

template<>
struct factorial< 0 > { enum { ret = 1 }; };

int main() {
    cout << "7! = " << factorial< 7 >::ret << endl; // 5040
    return 0;
}
3 Answers

Template metaprogramming is in theory a pure functional oriented language, i.e., functions only depend on their arguments, regardless of any global or local state. However, it appears that we can capture and retrieve a metaprogramming state using friend injections. Therefore, functions can return different result depending of a global/local metaprogramming state.

Related