Please sign in to access this page

Typename Array

Typename Array Used AI

5 devlogs
8h 25m
Created by b068931

Advanced C++ metaprogramming by interpreting template instances as arrays of types. Provides a bunch of helpful headers and an example: static-calculator (parses and computes simple expressions using nothing but C++'s type system).

Timeline

Ship 1

b068931

3 months ago

b068931 Covers 5 devlogs and 8h 25m

What I often forget when I do template metaprogramming is the fact that there is no concept of order of execution or short circuiting (in conditionals) altogether. Moreover, there are no lazy computations: all statements get computed no matter what. This is the beauty of template metaprogramming, when you look at each template you must be sure that it is always valid in its entirety for all valid inputs. Otherwise, you end up something like error-type and the program won't compile even if you never use said error-type.

Update attachment

Sometimes people say that C++'s type system can be considered a functional language, but why? I believe it was enabled by type deduction in partial specializations and recursion (the ability of a template to use itself for own definition). The first part essentially performs binding for these template lambdas, while recursion enables reduction (or evaluation) of these templates. And template can be reduced to anything: another type, compile time constant, etc. For example, it is possible to implement merge sort in C++ using its type system. Moreover, this implementation works on MSVC, GCC, Clang.

Update attachment

Just implemented another batch of tests for some of the primitives. Actually, the reason why I bother with this project is mostly because I intend to use it as a part of FSI. Specifically, typename array is used for packaging the data when it is transferred between module boundaries. I thought that if I am spending this much time on it, refactoring it, might as well add it as another project for Summer of Making.

Update attachment

Not even an hour has passed and I have already learned another intricacy of the C++ standard! I don't really like to delve much into the text itself, so when I am developing the thing I assume that my code is standard compliant as long as three major compilers are OK with it: MSVC, GCC, clang. I use template template parameters in order to iterate through elements functional-programming style (work with head and recursively iterate tail). Previously, I would use different template template type lists, based on what arguments I actually expect to use (e.g. template class arraytemplate or template class arraytemplate) and then partially specialize those accordingly, so that compiler can deduce the types and this was wrong. Apparently, either partial specializations must have identical template template arguments OR template template argument's list must match actual template argument list. One way or another, both MSVC and GCC would accept previous version, while clang accepts only homogeneous template template arguments. Notice in the image that I now use typename... everywhere.

Update attachment

Most certainly, this won't be useful to a lot of people. However, I must admit that this the project that I have the most fun working with. C++ is the only language (at least that I know of) that can even theoretically do something like this. Compilation times are long, and the fact that I develop inside a Windows VM running on Linux doesn't help.

Update attachment