is c++ Template Metaprogramming a form of functional programming

c++ functional programming
template metaprogramming c++17
why use template metaprogramming
c++ template metaprogramming book
c++ template programming tutorial
c++ metaprogramming type list
template metaprogramming vs constexpr
template metaprogramming factorial

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

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

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

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).

Because template metaprogramming is considered really hard. Both in C++ and D you use a form of functional language for that purpose. To start our discussion on metaprogramming, let’s go back to the era when the ANSI C programming language was a popular language. For simplicity, we used the C preprocessor by creating a macro. The C parameterized macro is also known as metafunctions and is one of the examples of metaprogramming. Abstract. Template metaprogramming is an emerging new direction C++ template metaprograms in the form of embedded Haskell code which is  Template metaprogramming (TMP) is a skill that evaluates the programmers who know it above those who don't. Troy introduces TMP in this lesson. Within C++ is another language, one which is purely functional.

Metaprogramming do all the business in compile time which has many pointer same with functional style. But they are not the same thing.

C++ templates: Creating a compile-time higher-order meta-programming language C++ templates are Turing-complete, we constructed a higher-order functional A template declares a data type parameterized by some constants and by  Although template metaprogramming has a strong relationship with functional programming, this is not reflected in the language syntax and existing libraries. In this paper we give a short and incomplete introduction to C++ templates and the basics of template metaprogramming.

Although template metaprogramming has a strong relationship with functional In C++, in order to use a template with some specific type, an instantiation. Template metaprogramming is in fact Turing-complete, meaning that any computation expressible by a computer program can be computed, in some form, by a template metaprogram. Template Metaprogramming is generally not used in practical programs, it is an interesting conecpt though.

This is the way programming works for the most part. With C++, we can also write a different kind of program—one that the compiler executes while it compiles  Surprisingly, the key to template metaprogramming turns out to be functional programming. Normal programs are written in an imperative style: the programmer tells the computer to do things in a certain order, and it goes away and executes them.

Here is my plan: I give an introduction to template metaprogramming, show how the type-traits library allows you the use template  at runtime there are libraries supporting functional programming [36] but in template metaprogramming current approaches [1, 25, 3] try to simulate imperative languages and libraries, such as the Standard Template Library of C++ [32, 41] and most of them does not take advantage of the functional paradigm. I.2 Structure of the dissertation

  • C++ templates are actually an extremely pure functional programming language (no side effects!), with the peculiarity of being evaluated entirely at compile time, unlike most functional languages. And the syntax is not very nice. So it makes sense that the same kind of techniques and algorithms that work with one also map to the other. In fact, there exist projects which "desugar" a subset of Haskell to C++ templates. I don't know very much about the performance characteristics / "operational semantics".
  • Maybe you should look at boost::phoenix.
  • This is definitely a real question. It's just one that is a little abstruse.
  • @Marcin : The description of 'not a real question' reads "This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form." -- I'd say that more than applies.
  • @ildjarn How is this question broad? It's very specific.
  • They are not completely different things. It would be closer to the truth to say they are the same thing: C++ template metaprogramming is functional programming. (Though the reverse, of course, is not true.)
  • The keypoints contradict your statement: c++ template metaprogramming and functional programming are not the same, but very close. In fact, you can parse a functional program in (a subset of) Haskell, transform and even execute it during compilation time in C++. E.g. see a library here that can do it.
  • You both make some good points, but I would still say they are completely different things at the high level. At the low level, they share some techincal aspects (like template metaprogramming uses subsets of functional style), but that's not really useful information if you want to consider the big picture.
  • At base, functional programming is programming consisting entirely of side-effect-less evaluation, which perfectly describes template metaprogramming. Looking for features like user input is beside the point; some functional languages (e.g. the lambda calculus) don't take user input.