ahala
ahala

Reputation: 4793

is c++ Template Metaprogramming a form of functional programming

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;
}

Upvotes: 15

Views: 5057

Answers (3)

Mechap
Mechap

Reputation: 349

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.

Upvotes: 0

Sneftel
Sneftel

Reputation: 41474

is c++ Template Metaprogramming a form of functional programming?

Yep! Template expansion has no side effects, so it is pure-functional.

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

Absolutely. Factorial isn't a good demonstration of this, since the result will overflow long before your stack does, but long recursions can definitely cause the compiler to error out. Interestingly, however, compilers tend to implement templates in such a way that you get automatic memoization. For instance, a naively written implementation of the Fibonacci series will tend to compile in O(n) time rather than O(2^n).

Upvotes: 9

Tomas Petricek
Tomas Petricek

Reputation: 243061

I think the best answer is that the two concepts are completely different things. However, that does not really help, so here are some key points that I can think of:

  • Both C++ templates and (parametric polymorphism in) functional languages like ML are examples of generic programming and so it makes some sense to mention them together in a single question. In academia, there is even Workshop on Generic Programming that often covers C++ template stuff and ML-style functional programming.

  • However, template metaprogramming gives you a way to write code that gets "evaluated" during the compilation of your C++ code and so you can use it only for quite limited tasks. On the other hand, functional programs normally run, well, at runtime and you can define complex data structures, build abstractions etc. (you can do some things using template metaprogramming, but it will look ugly).

  • Why is the factorial example similar to factorials in functional programming? When you're using template meta-programming, you cannot define "mutable variables" (because you are just defining new types or templates) and so template meta-programming might feel a bit like functional programming style.

  • Code written using template metaprogramming is evaluated at compile-time. This means that it cannot depend on user input! It either compiles or not (the compiler may have some restriction on the depth of recursion and give up after some time). On the other hand, functional programs can take some input and then evaluate (which means they can use all the stack or memory available and then fail).

For functional programmers, I know that some functional languages like Agda can also evaluate things at compile-time, but I think it is better to keep things simple!

Upvotes: 7

Related Questions