This really is not the answer to your question. These are more observations on the side.
I am also not a lawyer in C ++, and therefore I could be outside the database with some details.
But a gross idea must be right.
The main reason C ++ compilers take so long to compile template metaprograms is due to how template metaprograms are defined.
They are not specified directly as the code that you want the compiler to execute at compile time. Take an example of calculating the length of a type list.
If you could write code like this:
compile_time size_t GetLength(TypeList * pTypeList) { return DoGetLength(pTypeList, 0); } compile_time size_t DoGetLength(TypeList * pTypeList, size_t currentLength) { if (pTypeList) { return DoGetLength(pTypeList->Next, ++currentLength); } else { return currentLength; } }
Here are some of them that were compiled separately from the code in which it was used, and were exposed to the language through some syntax, then the compiler will be able to execute it very quickly.
This is just a call to a simple recursive function.
You can create a language that allows such things. Most of them that do this (e.g. lisp) are dynamically typed, but this can be done with a static set. However, this is unlikely to ever be what you see in C ++.
However, the problem in C ++ is that the code is written as:
template <typename First, typename Second> struct TypeList { typedef First Head; typedef Second Tail; }; template <> struct ListSize<NullType> { enum { size = 0 }; }; template <typename Head, typename Tail> struct ListSize<TypeList<Head, Tail> > { enum { size = 1 + ListSize<Tail>::size }; };
For the compiler to “execute” the metaprogram, it must:
- Build a dependency graph for the initial values of the size value "size"
- Build a template type for each edge in a graph
- Bind all characters referenced by each template type built
- Topologically sort the dependency graph
- Go through the graph and evaluate the constants
This is much more expensive than just running the recursive O (N) algorithm.
In the worst case, there will be something like O (N * M * L), where N is the length of the list, M is the nesting level of the area, and L is the number of characters in each area.
My advice will be to minimize the amount of C ++ template metaprogram code that you use.