C++ Template compiler

High performance compile time c++ templates


When C++ and templates occur in a single sentence people mostly talk about generic programming. This however talks about templates used to generate text based on a given definition. This programm allows one to transform a template written in a syntax similar to the one used by Twig into an optimized C++ class. It supports a huge portion of the functionality provided by Twig and extends it with C++ specific extensions where it made sense to do so. The generate code uses only STL classes and do not depend on any additional libraries. This allows on to easily integrate the generated templates into existing software. Unlike other template libraries this does not support runtime evaluation of templates. If your search for a library that supports this, you might want to check out inja.

Implemented functions include:

  • Variable substitution
  • Loops
  • Conditions
  • Comments
  • Inheritance
  • Codeblocks (Arbitrary C++ code)
  • C++ includes
  • Parameter
  • Variables
  • Namespaces


{% #include  %}
{% #include_impl  %}
{% variable a_variable TestVariable std::string %}
{% param idx int %}
{% namespace templates %}
{% init %}
std::cout<< "template init" << std::endl;
{% endinit %}
{{ a_variable }}
{% block testBlock %}
Compile Datetime: {{ __compile_datetime__ }}
{{ std::to_string(idx) }} {% endblock %}

As you can see in the example there are two different options to provide values inside the templates. There are both variables as well as parameters. The difference is in there scope. Variables are generated as a classmember, which means they will stay in memory as long as the template exists. Parameters however are supplied on a per render basis. This allows reusing a single instance of the template from multiple threads, sharing some of the data while.