Is a C ++ Template Metaprogramming Form of Functional Programming

- C ++ template. Metaprogramming a form of functional programming? If so, do some pitfalls, such as stackoverflow for irregular recursion, relevant for metaprogramming C ++ templates?

For an example factorial in this question , I think this is standard functional programming. Or is the similarity 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; } 
+7
source share
2 answers

- C ++ template. Metaprogramming a form of functional programming?

Oops! The extension of the template has no side effects, so it is purely functional.

If so, do some traps like stackoverflow for irregular recursion relevant for metaprogramming C ++ templates?

That's right. Factorial is not a good demonstration of this, as the result will overflow long before your stack, but long recursions can lead to a compiler error. Interestingly, however, compilers tend to implement templates in such a way that you get automatic memoization. For example, a naively written Fibonacci series implementation will seek to compile in O (n) time, rather than O (2 ^ n).

+2
source

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

  • Both C ++ patterns and (parametric polymorphism in) functional languages, such as ML, are examples of universal programming , and therefore it makes sense to mention them together in one question. In academic circles, there is even a Universal Programming Seminar , which often covers elements of the C ++ template and functional programming of the ML-style.

  • However, template metaprogramming gives you a way to write code that gets an “evaluation” when compiling your C ++ code, and therefore you can use it only for fairly limited tasks. On the other hand, functional programs usually 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 factorial similar to factorials in functional programming? When you use meta-programming of templates, you cannot define “mutable variables” (because you simply define new types or templates), and therefore meta-programming of templates may seem a bit like a 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 recursion depth and abandon it after some time). Functional programs, on the other hand, can take some input and then evaluate (which means that they can use the entire stack or memory, and then crash).

For functional programmers, I know that some functional languages, such as Agda , can also evaluate things at compile time, but I think it's best to keep things simple!

+3
source

All Articles