Jump to content

Template metaprogramming: Difference between revisions

From Wikipedia, the free encyclopedia
Content deleted Content added
Added information about role of concepts in template meta programming with example, that illustrates how proper specialization is choosen. It's my first remark in codin, so i will be pleased for review of more advanced wiki user
Citation bot (talk | contribs)
Added publisher. | Use this bot. Report bugs. | Suggested by Dominic3203 | Category:Metaprogramming | #UCB_Category 7/10
 
(29 intermediate revisions by 24 users not shown)
Line 1: Line 1:
{{Short description|Metaprogramming technique}}
{{More footnotes|date=June 2010}}
{{More footnotes needed|date=June 2010}}
{{Programming paradigms}}


'''Template metaprogramming''' ('''TMP''') is a [[metaprogramming]] technique in which [[Generic programming|templates]] are used by a [[compiler]] to generate temporary [[source code]], which is merged by the compiler with the rest of the source code and then compiled. The output of these templates include [[compile time|compile-time]] [[constant (programming)|constant]]s, [[data structure]]s, and complete [[function (computer science)|function]]s. The use of templates can be thought of as [[Compile-time function execution|compile-time polymorphism]]. The technique is used by a number of languages, the best-known being [[C++]], but also [[Curl programming language|Curl]], [[D programming language|D]], and [[XL Programming Language|XL]].
'''Template metaprogramming''' ('''TMP''') is a [[metaprogramming]] technique in which [[Generic programming|templates]] are used by a [[compiler]] to generate temporary [[source code]], which is merged by the compiler with the rest of the source code and then compiled. The output of these templates can include [[compile time|compile-time]] [[constant (programming)|constant]]s, [[data structure]]s, and complete [[function (computer science)|function]]s. The use of templates can be thought of as [[Compile-time function execution|compile-time polymorphism]]. The technique is used by a number of languages, the best-known being [[C++]], but also [[Curl programming language|Curl]], [[D programming language|D]], [[Nim (programming language)|Nim]], and [[XL Programming Language|XL]].


Template metaprogramming was, in a sense, discovered accidentally.<ref name="Meyers2005">{{cite book|author=Scott Meyers|title=Effective C++: 55 Specific Ways to Improve Your Programs and Designs|url=https://books.google.com/books?id=Qx5oyB49poYC&q=%22Template+metaprogramming%22|date=12 May 2005|publisher=Pearson Education|isbn=978-0-13-270206-5}}</ref><ref>See [[wikibooks:C++ Programming/Templates/Template Meta-Programming#History of TMP|History of TMP]] on Wikibooks</ref>
Template metaprogramming was, in a sense, discovered accidentally.<ref name="Meyers2005">{{cite book|author=Scott Meyers|title=Effective C++: 55 Specific Ways to Improve Your Programs and Designs|url=https://books.google.com/books?id=Qx5oyB49poYC&q=%22Template+metaprogramming%22|date=12 May 2005|publisher=Pearson Education|isbn=978-0-13-270206-5}}</ref><ref>See [[wikibooks:C++ Programming/Templates/Template Meta-Programming#History of TMP|History of TMP]] on Wikibooks</ref>
Line 9: Line 9:


==Components of template metaprogramming==
==Components of template metaprogramming==
The use of templates as a metaprogramming technique requires two distinct operations: a template must be defined, and a defined template must be [[Instance (computer science)|instantiated]]. The template definition describes the generic form of the generated source code, and the instantiation causes a specific set of source code to be generated from the generic form in the template.
The use of templates as a metaprogramming technique requires two distinct operations: a template must be defined, and a defined template must be [[Instance (computer science)|instantiated]]. The generic form of the generated source code is described in the template definition, and when the template is instantiated, the generic form in the template is used to generate a specific set of source code.


Template metaprogramming is [[Turing-complete]], meaning that any computation expressible by a computer program can be computed, in some form, by a template metaprogram.<ref name=Veldhuizen2003>{{cite document|last1=Veldhuizen|first1=Todd L.|title=C++ Templates are Turing Complete|citeseerx=10.1.1.14.3670}}</ref>
Template metaprogramming is [[Turing-complete]], meaning that any computation expressible by a computer program can be computed, in some form, by a template metaprogram.<ref name=Veldhuizen2003>{{cite CiteSeerX|last1=Veldhuizen|first1=Todd L.|title=C++ Templates are Turing Complete|year=2003|citeseerx=10.1.1.14.3670}}</ref>


Templates are different from ''[[Macro (computer science)#Programming macros|macros]]''. A macro is a piece of code that executes at compile time and either performs textual manipulation of code to-be compiled (e.g. [[C++]] macros) or manipulates the [[abstract syntax tree]] being produced by the compiler (e.g. [[Rust (programming language)|Rust]] or [[Lisp (programming language)|Lisp]] macros). Textual macros are notably more independent of the syntax of the language being manipulated, as they merely change the in-memory text of the source code right before compilation.
Templates are different from ''[[Macro (computer science)#Programming macros|macros]]''. A macro is a piece of code that executes at compile time and either performs textual manipulation of code to-be compiled (e.g. [[C++]] macros) or manipulates the [[abstract syntax tree]] being produced by the compiler (e.g. [[Rust (programming language)|Rust]] or [[Lisp (programming language)|Lisp]] macros). Textual macros are notably more independent of the syntax of the language being manipulated, as they merely change the in-memory text of the source code right before compilation.
Line 23: Line 23:
What exactly "programming at compile-time" means can be illustrated with an example of a factorial function, which in non-template C++ can be written using recursion as follows:
What exactly "programming at compile-time" means can be illustrated with an example of a factorial function, which in non-template C++ can be written using recursion as follows:
<syntaxhighlight lang=cpp>
<syntaxhighlight lang=cpp>
unsigned int factorial(unsigned int n) {
unsigned factorial(unsigned n) {
return n == 0 ? 1 : n * factorial(n - 1);
return n == 0 ? 1 : n * factorial(n - 1);
}
}
Line 31: Line 31:
// factorial(4) would yield 24.
// factorial(4) would yield 24.
</syntaxhighlight>
</syntaxhighlight>
The code above will execute at run time to determine the factorial value of the literals 4 and 0.
The code above will execute at run time to determine the factorial value of the literals 0 and 4.
By using template metaprogramming and template specialization to provide the ending condition for the recursion, the factorials used in the program—ignoring any factorial not used—can be calculated at compile time by this code:
By using template metaprogramming and template specialization to provide the ending condition for the recursion, the factorials used in the program—ignoring any factorial not used—can be calculated at compile time by this code:
<syntaxhighlight lang=cpp>
<syntaxhighlight lang=cpp>
template <unsigned int n>
template <unsigned N>
struct factorial {
struct factorial {
enum { value = n * factorial<n - 1>::value };
static constexpr unsigned value = N * factorial<N - 1>::value;
};
};


template <>
template <>
struct factorial<0> {
struct factorial<0> {
enum { value = 1 };
static constexpr unsigned value = 1;
};
};


Line 48: Line 48:
// factorial<4>::value would yield 24.
// factorial<4>::value would yield 24.
</syntaxhighlight>
</syntaxhighlight>
The code above calculates the factorial value of the literals 4 and 0 at compile time and uses the results as if they were precalculated constants.
The code above calculates the factorial value of the literals 0 and 4 at compile time and uses the results as if they were precalculated constants.
To be able to use templates in this manner, the compiler must know the value of its parameters at compile time, which has the natural precondition that factorial<X>::value can only be used if X is known at compile time. In other words, X must be a constant literal or a constant expression.
To be able to use templates in this manner, the compiler must know the value of its parameters at compile time, which has the natural precondition that factorial<X>::value can only be used if X is known at compile time. In other words, X must be a constant literal or a constant expression.


In [[C++11]] and [[C++20]], [[constexpr]] and consteval were introduced to let the compiler execute code. Using constexpr and consteval, one can use the usual recursive factorial definition with the non-templated syntax.<ref>http://www.cprogramming.com/c++11/c++11-compile-time-processing-with-constexpr.html</ref>
In [[C++11]] and [[C++20]], [[constexpr]] and consteval were introduced to let the compiler execute code. Using constexpr and consteval, one can use the usual recursive factorial definition with the non-templated syntax.<ref>{{Cite web|url=https://www.cprogramming.com/c++11/c++11-compile-time-processing-with-constexpr.html|title=Constexpr - Generalized Constant Expressions in C++11 - Cprogramming.com|website=www.cprogramming.com}}</ref>


==Compile-time code optimization==
==Compile-time code optimization==
{{see also|Compile-time function execution}}
{{see also|Compile-time function execution}}
The factorial example above is one example of compile-time code optimization in that all factorials used by the program are pre-compiled and injected as numeric constants at compilation, saving both run-time overhead and memory footprint. It is, however, a relatively minor optimization.
The factorial example above is one example of compile-time code optimization in that all factorials used by the program are pre-compiled and injected as numeric constants at compilation, saving both run-time overhead and [[memory footprint]]. It is, however, a relatively minor optimization.


As another, more significant, example of compile-time [[loop unrolling]], template metaprogramming can be used to create length-''n'' vector classes (where ''n'' is known at compile time). The benefit over a more traditional length-''n'' vector is that the loops can be unrolled, resulting in very optimized code. As an example, consider the addition operator. A length-''n'' vector addition might be written as
As another, more significant, example of compile-time [[loop unrolling]], template metaprogramming can be used to create length-''n'' vector classes (where ''n'' is known at compile time). The benefit over a more traditional length-''n'' vector is that the loops can be unrolled, resulting in very optimized code. As an example, consider the addition operator. A length-''n'' vector addition might be written as
Line 131: Line 131:
};
};
</syntaxhighlight>
</syntaxhighlight>
Here the base class template will take advantage of the fact that member function bodies are not instantiated until after their declarations, and it will use members of the derived class within its own member functions, via the use of a <code>static_cast</code>, thus at compilation generating an object composition with polymorphic characteristics. As an example of real-world usage, the CRTP is used in the [[Boost library|Boost]] [[iterator]] library.<ref>http://www.boost.org/libs/iterator/doc/iterator_facade.html</ref>
Here the base class template will take advantage of the fact that member function bodies are not instantiated until after their declarations, and it will use members of the derived class within its own member functions, via the use of a <code>static_cast</code>, thus at compilation generating an object composition with polymorphic characteristics. As an example of real-world usage, the CRTP is used in the [[Boost library|Boost]] [[iterator]] library.<ref>{{Cite web|url=http://www.boost.org/libs/iterator/doc/iterator_facade.html|title = Iterator Facade - 1.79.0}}</ref>


Another similar use is the "[[Barton–Nackman trick]]", sometimes referred to as "restricted template expansion", where common functionality can be placed in a base class that is used not as a contract but as a necessary component to enforce conformant behaviour while minimising code redundancy.
Another similar use is the "[[Barton–Nackman trick]]", sometimes referred to as "restricted template expansion", where common functionality can be placed in a base class that is used not as a contract but as a necessary component to enforce conformant behaviour while minimising code redundancy.
Line 167: Line 167:


int main() {
int main() {
for(int i=0; i < TABLE_SIZE; i++) {
for (int i=0; i < TABLE_SIZE; i++) {
std::cout << table[i] << std::endl; // run time use
std::cout << table[i] << std::endl; // run time use
}
}
Line 224: Line 224:


int main() {
int main() {
for(int i=0; i < TABLE_SIZE; i++) {
for (int i=0; i < TABLE_SIZE; i++) {
std::cout << table[i] << std::endl; // run time use
std::cout << table[i] << std::endl; // run time use
}
}
std::cout << "FOUR: " << FOUR << std::endl;
std::cout << "FOUR: " << FOUR << std::endl;
}
}
</syntaxhighlight>
</syntaxhighlight>


To show a more sophisticated example the code in the following listing has been extended to have a helper for value calculation (in preparation for more complicated computations), a table specific offset and a template argument for the type of the table values (e.g. uint8_t, uint16_t, ...).
To show a more sophisticated example the code in the following listing has been extended to have a helper for value calculation (in preparation for more complicated computations), a table specific offset and a template argument for the type of the table values (e.g. uint8_t, uint16_t, ...).
Line 265: Line 265:


int main() {
int main() {
for(int i = 0; i < TABLE_SIZE; i++) {
for (int i = 0; i < TABLE_SIZE; i++) {
std::cout << table[i] << std::endl;
std::cout << table[i] << std::endl;
}
}
Line 278: Line 278:
constexpr int OFFSET = 12;
constexpr int OFFSET = 12;


template<typename VALUETYPE, VALUETYPE OFFSET>
template<typename VALUETYPE, int OFFSET>
constexpr std::array<VALUETYPE, TABLE_SIZE> table = [] { // OR: constexpr auto table
constexpr std::array<VALUETYPE, TABLE_SIZE> table = [] { // OR: constexpr auto table
std::array<VALUETYPE, TABLE_SIZE> A = {};
std::array<VALUETYPE, TABLE_SIZE> A = {};
Line 288: Line 288:


int main() {
int main() {
for(int i = 0; i < TABLE_SIZE; i++) {
for (int i = 0; i < TABLE_SIZE; i++) {
std::cout << table<uint16_t, OFFSET>[i] << std::endl;
std::cout << table<uint16_t, OFFSET>[i] << std::endl;
}
}
Line 294: Line 294:
</syntaxhighlight>
</syntaxhighlight>


==Concepts==
==New possibilites in metaprogramming thanks to concepts==
C++20 standard brings C++ programmers new tools for meta template programming, which are concepts<ref>https://en.cppreference.com/enwiki/w/cpp/language/constraints</ref>.
The C++20 standard brought C++ programmers a new tool for meta template programming, concepts.<ref>{{Cite web|url=https://en.cppreference.com/enwiki/w/cpp/language/constraints|title=Constraints and concepts (since C++20) - cppreference.com|website=en.cppreference.com}}</ref>


Thanks to them, now programmer can specify requirements for type, to make instantation of template possible.
[[Concepts (C++)|Concepts]] allow programmers to specify requirements for the type, to make instantiation of template possible. The compiler looks for a template with the concept that has the highest requirements.
Moreover standard says, that compiler should look for template with concpet, that has highest requirements.


Here is example of famous [[Fizz buzz]] problem fully resolved with Template Meta Programming
Here is an example of the famous [[Fizz buzz]] problem solved with Template Meta Programming.


<syntaxhighlight lang="cpp">
<syntaxhighlight lang="cpp">
Line 324: Line 323:


/**
/**
* By specializing `res` structure, with concepts requirements, proper instantation is performed
* By specializing `res` structure, with concepts requirements, proper instantiation is performed
*/
*/
template<typename X> struct res;
template<typename X> struct res;
Line 333: Line 332:


/**
/**
* Predeclaration of concentrator
* Predeclaration of concatenator
*/
*/
template <size_t cnt, typename... Args>
template <size_t cnt, typename... Args>
Line 368: Line 367:


==Benefits and drawbacks of template metaprogramming==
==Benefits and drawbacks of template metaprogramming==
; Compile-time versus execution-time tradeoff : If a great deal of template metaprogramming is used.
Compile-time versus execution-time tradeoffs get visible if a great deal of template metaprogramming is used.
; [[Generic programming]] : Template metaprogramming allows the programmer to focus on architecture and delegate to the compiler the generation of any implementation required by client code. Thus, template metaprogramming can accomplish truly generic code, facilitating code minimization and better maintainability{{Citation needed|date=June 2014}}.
* Template metaprogramming allows the programmer to focus on architecture and delegate to the compiler the generation of any implementation required by client code. Thus, template metaprogramming can accomplish truly [[Generic programming|generic code]], facilitating code minimization and better maintainability{{Citation needed|date=June 2014}}.
; Readability : With respect to C++, the syntax and idioms of template metaprogramming are esoteric compared to conventional C++ programming, and template metaprograms can be very difficult to understand.<ref>{{cite document
* With respect to C++ prior to version ''C++11'', the syntax and idioms of template metaprogramming were esoteric compared to conventional C++ programming, and template metaprograms could be very difficult to understand.<ref>{{cite web
| first1 = K.
| first1 = K.
| last1 = Czarnecki
| last1 = Czarnecki
Line 385: Line 384:
|quote=''C++ Template Metaprogramming suffers from a number of limitations, including portability problems due to compiler limitations (although this has significantly improved in the last few years), lack of debugging support or IO during template instantiation, long compilation times, long and incomprehensible errors, poor readability of the code, and poor error reporting.''
|quote=''C++ Template Metaprogramming suffers from a number of limitations, including portability problems due to compiler limitations (although this has significantly improved in the last few years), lack of debugging support or IO during template instantiation, long compilation times, long and incomprehensible errors, poor readability of the code, and poor error reporting.''
| ref = Czarnecki, O’Donnell, Striegnitz, Taha - DSL implementation in metaocaml, template haskell, and C++
| ref = Czarnecki, O’Donnell, Striegnitz, Taha - DSL implementation in metaocaml, template haskell, and C++
}}</ref><ref>{{cite document
}}</ref><ref>{{cite web
| first1 = Tim
| first1 = Tim
| last1 = Sheard
| last1 = Sheard
Line 397: Line 396:
| quote = ''Robinson’s provocative paper identifies C++ templates as a major, albeit accidental, success of the C++ language design. Despite the extremely baroque nature of template meta-programming, templates are used in fascinating ways that extend beyond the wildest dreams of the language designers. Perhaps surprisingly, in view of the fact that templates are functional programs, functional programmers have been slow to capitalize on C++’s success''
| quote = ''Robinson’s provocative paper identifies C++ templates as a major, albeit accidental, success of the C++ language design. Despite the extremely baroque nature of template meta-programming, templates are used in fascinating ways that extend beyond the wildest dreams of the language designers. Perhaps surprisingly, in view of the fact that templates are functional programs, functional programmers have been slow to capitalize on C++’s success''
| ref = Sheard, S.P.Jones - Template Meta-programming for Haskell
| ref = Sheard, S.P.Jones - Template Meta-programming for Haskell
}}</ref> But from C++11 onward the syntax for value computation metaprogramming becomes more and more akin to "normal" C++, with less and less readability penalty.
}}</ref>


==See also==
==See also==
Line 405: Line 404:
* [[Parametric polymorphism]]
* [[Parametric polymorphism]]
* [[Expression templates]]
* [[Expression templates]]
* [[Variadic Templates]]
* [[Variadic template]]
* [[Compile-time function execution]]
* [[Compile-time function execution]]


Line 414: Line 413:
* {{cite book | authorlink1 = David Abrahams (computer programmer) | first1 = David | last1 = Abrahams | authorlink2 = Aleksey Gurtovoy | first2 = Aleksey | last2 = Gurtovoy | title = C++ Template Metaprogramming: Concepts, Tools, and Techniques from Boost and Beyond | publisher = Addison-Wesley | isbn = 0-321-22725-5 | date = January 2005 }}
* {{cite book | authorlink1 = David Abrahams (computer programmer) | first1 = David | last1 = Abrahams | authorlink2 = Aleksey Gurtovoy | first2 = Aleksey | last2 = Gurtovoy | title = C++ Template Metaprogramming: Concepts, Tools, and Techniques from Boost and Beyond | publisher = Addison-Wesley | isbn = 0-321-22725-5 | date = January 2005 }}
* {{cite book | authorlink1 = David Vandevoorde | first1 = David | last1 = Vandevoorde | authorlink2 = Nicolai M. Josuttis | first2 = Nicolai M. | last2 = Josuttis | title = C++ Templates: The Complete Guide | publisher = Addison-Wesley | isbn = 0-201-73484-2 | year = 2003 }}
* {{cite book | authorlink1 = David Vandevoorde | first1 = David | last1 = Vandevoorde | authorlink2 = Nicolai M. Josuttis | first2 = Nicolai M. | last2 = Josuttis | title = C++ Templates: The Complete Guide | publisher = Addison-Wesley | isbn = 0-201-73484-2 | year = 2003 }}
* {{cite book | authorlink = Manuel Clavel | first = Manuel | last = Clavel | title = Reflection in Rewriting Logic: Metalogical Foundations and Metaprogramming Applications | isbn = 1-57586-238-7 | date = 2000-10-16 }}
* {{cite book | authorlink = Manuel Clavel | first = Manuel | last = Clavel | title = Reflection in Rewriting Logic: Metalogical Foundations and Metaprogramming Applications | isbn = 1-57586-238-7 | date = 2000-10-16 | publisher = Cambridge University Press }}


==External links==
==External links==
Line 425: Line 424:
* {{cite web | url = http://staff.ustc.edu.cn/~xyfeng/teaching/FOPL/lectureNotes/MetaprogrammingCpp.pdf | title = Metaprogramming in C++ | authorlink = Johannes Koskinen | first = Johannes | last = Koskinen }}
* {{cite web | url = http://staff.ustc.edu.cn/~xyfeng/teaching/FOPL/lectureNotes/MetaprogrammingCpp.pdf | title = Metaprogramming in C++ | authorlink = Johannes Koskinen | first = Johannes | last = Koskinen }}
* {{cite web | url = http://lcgapp.cern.ch/project/architecture/ReflectionPaper.pdf | title = Reflection support by means of template metaprogramming | first1 = Giuseppe | last1 = Attardi | first2 = Antonio | last2 = Cisternino }}
* {{cite web | url = http://lcgapp.cern.ch/project/architecture/ReflectionPaper.pdf | title = Reflection support by means of template metaprogramming | first1 = Giuseppe | last1 = Attardi | first2 = Antonio | last2 = Cisternino }}
* {{cite document | citeseerx = 10.1.1.14.5881 | title = Static data structures | first1 = Michael C. | last1 = Burton | first2 = William G. | last2 = Griswold | first3 = Andrew D. | last3 = McCulloch | first4 = Gary A. | last4 = Huber }}
* {{cite CiteSeerX | citeseerx = 10.1.1.14.5881 | title = Static data structures | first1 = Michael C. | last1 = Burton | first2 = William G. | last2 = Griswold | first3 = Andrew D. | last3 = McCulloch | first4 = Gary A. | last4 = Huber | year = 2002 }}
* {{cite web | url = http://www.codeproject.com/Articles/19989/Template-Meta-Programming-and-Number-Theory | title = Template Meta Programming and Number Theory | first = Zeeshan | last = Amjad }}
* {{cite web | url = http://www.codeproject.com/Articles/19989/Template-Meta-Programming-and-Number-Theory | title = Template Meta Programming and Number Theory | first = Zeeshan | last = Amjad | date = 13 August 2007 }}
* {{cite web | url = http://www.codeproject.com/Articles/20180/Template-Meta-Programming-and-Number-Theory-Part | title = Template Meta Programming and Number Theory: Part 2 | first = Zeeshan | last = Amjad }}
* {{cite web | url = http://www.codeproject.com/Articles/20180/Template-Meta-Programming-and-Number-Theory-Part | title = Template Meta Programming and Number Theory: Part 2 | first = Zeeshan | last = Amjad | date = 24 August 2007 }}
* {{cite web | url = http://www.intelib.org/intro.html | title = A library for LISP-style programming in C++ }}
* {{cite web | url = http://www.intelib.org/intro.html | title = A library for LISP-style programming in C++ }}

{{Programming paradigms navbox}}


[[Category:Metaprogramming]]
[[Category:Metaprogramming]]

Latest revision as of 12:54, 29 November 2024

Template metaprogramming (TMP) is a metaprogramming technique in which templates are used by a compiler to generate temporary source code, which is merged by the compiler with the rest of the source code and then compiled. The output of these templates can include compile-time constants, data structures, and complete functions. The use of templates can be thought of as compile-time polymorphism. The technique is used by a number of languages, the best-known being C++, but also Curl, D, Nim, and XL.

Template metaprogramming was, in a sense, discovered accidentally.[1][2]

Some other languages support similar, if not more powerful, compile-time facilities (such as Lisp macros), but those are outside the scope of this article.

Components of template metaprogramming

[edit]

The use of templates as a metaprogramming technique requires two distinct operations: a template must be defined, and a defined template must be instantiated. The generic form of the generated source code is described in the template definition, and when the template is instantiated, the generic form in the template is used to generate a specific set of source code.

Template metaprogramming is Turing-complete, meaning that any computation expressible by a computer program can be computed, in some form, by a template metaprogram.[3]

Templates are different from macros. A macro is a piece of code that executes at compile time and either performs textual manipulation of code to-be compiled (e.g. C++ macros) or manipulates the abstract syntax tree being produced by the compiler (e.g. Rust or Lisp macros). Textual macros are notably more independent of the syntax of the language being manipulated, as they merely change the in-memory text of the source code right before compilation.

Template metaprograms have no mutable variables— that is, no variable can change value once it has been initialized, therefore template metaprogramming can be seen as a form of functional programming. In fact many template implementations implement flow control only through recursion, as seen in the example below.

Using template metaprogramming

[edit]

Though the syntax of template metaprogramming is usually very different from the programming language it is used with, it has practical uses. Some common reasons to use templates are to implement generic programming (avoiding sections of code which are similar except for some minor variations) or to perform automatic compile-time optimization such as doing something once at compile time rather than every time the program is run — for instance, by having the compiler unroll loops to eliminate jumps and loop count decrements whenever the program is executed.

Compile-time class generation

[edit]

What exactly "programming at compile-time" means can be illustrated with an example of a factorial function, which in non-template C++ can be written using recursion as follows:

unsigned factorial(unsigned n) {
	return n == 0 ? 1 : n * factorial(n - 1); 
}

// Usage examples:
// factorial(0) would yield 1;
// factorial(4) would yield 24.

The code above will execute at run time to determine the factorial value of the literals 0 and 4. By using template metaprogramming and template specialization to provide the ending condition for the recursion, the factorials used in the program—ignoring any factorial not used—can be calculated at compile time by this code:

template <unsigned N>
struct factorial {
	static constexpr unsigned value = N * factorial<N - 1>::value;
};

template <>
struct factorial<0> {
	static constexpr unsigned value = 1;
};

// Usage examples:
// factorial<0>::value would yield 1;
// factorial<4>::value would yield 24.

The code above calculates the factorial value of the literals 0 and 4 at compile time and uses the results as if they were precalculated constants. To be able to use templates in this manner, the compiler must know the value of its parameters at compile time, which has the natural precondition that factorial<X>::value can only be used if X is known at compile time. In other words, X must be a constant literal or a constant expression.

In C++11 and C++20, constexpr and consteval were introduced to let the compiler execute code. Using constexpr and consteval, one can use the usual recursive factorial definition with the non-templated syntax.[4]

Compile-time code optimization

[edit]

The factorial example above is one example of compile-time code optimization in that all factorials used by the program are pre-compiled and injected as numeric constants at compilation, saving both run-time overhead and memory footprint. It is, however, a relatively minor optimization.

As another, more significant, example of compile-time loop unrolling, template metaprogramming can be used to create length-n vector classes (where n is known at compile time). The benefit over a more traditional length-n vector is that the loops can be unrolled, resulting in very optimized code. As an example, consider the addition operator. A length-n vector addition might be written as

template <int length>
Vector<length>& Vector<length>::operator+=(const Vector<length>& rhs) 
{
    for (int i = 0; i < length; ++i)
        value[i] += rhs.value[i];
    return *this;
}

When the compiler instantiates the function template defined above, the following code may be produced:[citation needed]

template <>
Vector<2>& Vector<2>::operator+=(const Vector<2>& rhs) 
{
    value[0] += rhs.value[0];
    value[1] += rhs.value[1];
    return *this;
}

The compiler's optimizer should be able to unroll the for loop because the template parameter length is a constant at compile time.

However, take care and exercise caution as this may cause code bloat as separate unrolled code will be generated for each 'N'(vector size) you instantiate with.

Static polymorphism

[edit]

Polymorphism is a common standard programming facility where derived objects can be used as instances of their base object but where the derived objects' methods will be invoked, as in this code

class Base
{
public:
    virtual void method() { std::cout << "Base"; }
    virtual ~Base() {}
};

class Derived : public Base
{
public:
    virtual void method() { std::cout << "Derived"; }
};

int main()
{
    Base *pBase = new Derived;
    pBase->method(); //outputs "Derived"
    delete pBase;
    return 0;
}

where all invocations of virtual methods will be those of the most-derived class. This dynamically polymorphic behaviour is (typically) obtained by the creation of virtual look-up tables for classes with virtual methods, tables that are traversed at run time to identify the method to be invoked. Thus, run-time polymorphism necessarily entails execution overhead (though on modern architectures the overhead is small).

However, in many cases the polymorphic behaviour needed is invariant and can be determined at compile time. Then the Curiously Recurring Template Pattern (CRTP) can be used to achieve static polymorphism, which is an imitation of polymorphism in programming code but which is resolved at compile time and thus does away with run-time virtual-table lookups. For example:

template <class Derived>
struct base
{
    void interface()
    {
         // ...
         static_cast<Derived*>(this)->implementation();
         // ...
    }
};

struct derived : base<derived>
{
     void implementation()
     {
         // ...
     }
};

Here the base class template will take advantage of the fact that member function bodies are not instantiated until after their declarations, and it will use members of the derived class within its own member functions, via the use of a static_cast, thus at compilation generating an object composition with polymorphic characteristics. As an example of real-world usage, the CRTP is used in the Boost iterator library.[5]

Another similar use is the "Barton–Nackman trick", sometimes referred to as "restricted template expansion", where common functionality can be placed in a base class that is used not as a contract but as a necessary component to enforce conformant behaviour while minimising code redundancy.

Static Table Generation

[edit]

The benefit of static tables is the replacement of "expensive" calculations with a simple array indexing operation (for examples, see lookup table). In C++, there exists more than one way to generate a static table at compile time. The following listing shows an example of creating a very simple table by using recursive structs and variadic templates. The table has a size of ten. Each value is the square of the index.

#include <iostream>
#include <array>

constexpr int TABLE_SIZE = 10;

/**
 * Variadic template for a recursive helper struct.
 */
template<int INDEX = 0, int ...D>
struct Helper : Helper<INDEX + 1, D..., INDEX * INDEX> { };

/**
 * Specialization of the template to end the recursion when the table size reaches TABLE_SIZE.
 */
template<int ...D>
struct Helper<TABLE_SIZE, D...> {
  static constexpr std::array<int, TABLE_SIZE> table = { D... };
};

constexpr std::array<int, TABLE_SIZE> table = Helper<>::table;

enum  {
  FOUR = table[2] // compile time use
};

int main() {
  for (int i=0; i < TABLE_SIZE; i++) {
    std::cout << table[i]  << std::endl; // run time use
  }
  std::cout << "FOUR: " << FOUR << std::endl;
}

The idea behind this is that the struct Helper recursively inherits from a struct with one more template argument (in this example calculated as INDEX * INDEX) until the specialization of the template ends the recursion at a size of 10 elements. The specialization simply uses the variable argument list as elements for the array. The compiler will produce code similar to the following (taken from clang called with -Xclang -ast-print -fsyntax-only).

template <int INDEX = 0, int ...D> struct Helper : Helper<INDEX + 1, D..., INDEX * INDEX> {
};
template<> struct Helper<0, <>> : Helper<0 + 1, 0 * 0> {
};
template<> struct Helper<1, <0>> : Helper<1 + 1, 0, 1 * 1> {
};
template<> struct Helper<2, <0, 1>> : Helper<2 + 1, 0, 1, 2 * 2> {
};
template<> struct Helper<3, <0, 1, 4>> : Helper<3 + 1, 0, 1, 4, 3 * 3> {
};
template<> struct Helper<4, <0, 1, 4, 9>> : Helper<4 + 1, 0, 1, 4, 9, 4 * 4> {
};
template<> struct Helper<5, <0, 1, 4, 9, 16>> : Helper<5 + 1, 0, 1, 4, 9, 16, 5 * 5> {
};
template<> struct Helper<6, <0, 1, 4, 9, 16, 25>> : Helper<6 + 1, 0, 1, 4, 9, 16, 25, 6 * 6> {
};
template<> struct Helper<7, <0, 1, 4, 9, 16, 25, 36>> : Helper<7 + 1, 0, 1, 4, 9, 16, 25, 36, 7 * 7> {
};
template<> struct Helper<8, <0, 1, 4, 9, 16, 25, 36, 49>> : Helper<8 + 1, 0, 1, 4, 9, 16, 25, 36, 49, 8 * 8> {
};
template<> struct Helper<9, <0, 1, 4, 9, 16, 25, 36, 49, 64>> : Helper<9 + 1, 0, 1, 4, 9, 16, 25, 36, 49, 64, 9 * 9> {
};
template<> struct Helper<10, <0, 1, 4, 9, 16, 25, 36, 49, 64, 81>> {
  static constexpr std::array<int, TABLE_SIZE> table = {0, 1, 4, 9, 16, 25, 36, 49, 64, 81};
};

Since C++17 this can be more readably written as:

 
#include <iostream>
#include <array>

constexpr int TABLE_SIZE = 10;

constexpr std::array<int, TABLE_SIZE> table = [] { // OR: constexpr auto table
  std::array<int, TABLE_SIZE> A = {};
  for (unsigned i = 0; i < TABLE_SIZE; i++) {
    A[i] = i * i;
  }
  return A;
}();

enum  {
  FOUR = table[2] // compile time use
};

int main() {
  for (int i=0; i < TABLE_SIZE; i++) {
    std::cout << table[i]  << std::endl; // run time use
  }
  std::cout << "FOUR: " << FOUR << std::endl;
}

To show a more sophisticated example the code in the following listing has been extended to have a helper for value calculation (in preparation for more complicated computations), a table specific offset and a template argument for the type of the table values (e.g. uint8_t, uint16_t, ...).

                                                                
#include <iostream>
#include <array>

constexpr int TABLE_SIZE = 20;
constexpr int OFFSET = 12;

/**
 * Template to calculate a single table entry
 */
template <typename VALUETYPE, VALUETYPE OFFSET, VALUETYPE INDEX>
struct ValueHelper {
  static constexpr VALUETYPE value = OFFSET + INDEX * INDEX;
};

/**
 * Variadic template for a recursive helper struct.
 */
template<typename VALUETYPE, VALUETYPE OFFSET, int N = 0, VALUETYPE ...D>
struct Helper : Helper<VALUETYPE, OFFSET, N+1, D..., ValueHelper<VALUETYPE, OFFSET, N>::value> { };

/**
 * Specialization of the template to end the recursion when the table size reaches TABLE_SIZE.
 */
template<typename VALUETYPE, VALUETYPE OFFSET, VALUETYPE ...D>
struct Helper<VALUETYPE, OFFSET, TABLE_SIZE, D...> {
  static constexpr std::array<VALUETYPE, TABLE_SIZE> table = { D... };
};

constexpr std::array<uint16_t, TABLE_SIZE> table = Helper<uint16_t, OFFSET>::table;

int main() {
  for (int i = 0; i < TABLE_SIZE; i++) {
    std::cout << table[i] << std::endl;
  }
}

Which could be written as follows using C++17:

#include <iostream>
#include <array>

constexpr int TABLE_SIZE = 20;
constexpr int OFFSET = 12;

template<typename VALUETYPE, int OFFSET>
constexpr std::array<VALUETYPE, TABLE_SIZE> table = [] { // OR: constexpr auto table
  std::array<VALUETYPE, TABLE_SIZE> A = {};
  for (unsigned i = 0; i < TABLE_SIZE; i++) {
    A[i] = OFFSET + i * i;
  }
  return A;
}();

int main() {
  for (int i = 0; i < TABLE_SIZE; i++) {
    std::cout << table<uint16_t, OFFSET>[i] << std::endl;
  }
}

Concepts

[edit]

The C++20 standard brought C++ programmers a new tool for meta template programming, concepts.[6]

Concepts allow programmers to specify requirements for the type, to make instantiation of template possible. The compiler looks for a template with the concept that has the highest requirements.

Here is an example of the famous Fizz buzz problem solved with Template Meta Programming.

#include <boost/type_index.hpp> // for pretty printing of types
#include <iostream>
#include <tuple>

/**
 * Type representation of words to print
 */
struct Fizz {};
struct Buzz {};
struct FizzBuzz {};
template<size_t _N> struct number { constexpr static size_t N = _N; };

/**
 * Concepts used to define condition for specializations
 */
template<typename Any> concept has_N = requires{ requires Any::N - Any::N == 0; };
template<typename A> concept fizz_c = has_N<A> && requires{ requires A::N % 3 == 0; };
template<typename A> concept buzz_c = has_N<A> && requires{ requires A::N % 5 == 0;};
template<typename A> concept fizzbuzz_c = fizz_c<A> && buzz_c<A>;

/**
 * By specializing `res` structure, with concepts requirements, proper instantiation is performed
 */
template<typename X> struct res;
template<fizzbuzz_c X> struct res<X> { using result = FizzBuzz; };
template<fizz_c X> struct res<X> { using result = Fizz; };
template<buzz_c X> struct res<X> { using result = Buzz; };
template<has_N X> struct res<X> { using result = X; };

/**
 * Predeclaration of concatenator
 */
template <size_t cnt, typename... Args> 
struct concatenator;

/**
 * Recursive way of concatenating next types
 */
template <size_t cnt, typename ... Args>
struct concatenator<cnt, std::tuple<Args...>> 
{ using type = typename concatenator<cnt - 1, std::tuple< typename res< number<cnt> >::result, Args... >>::type;};

/**
 * Base case
 */
template <typename... Args> struct concatenator<0, std::tuple<Args...>> { using type = std::tuple<Args...>;};

/**
 * Final result getter
 */
template<size_t Amount>
using fizz_buzz_full_template = typename concatenator<Amount - 1, std::tuple<typename res<number<Amount>>::result>>::type;

int main()
{
	// printing result with boost, so it's clear
	std::cout << boost::typeindex::type_id<fizz_buzz_full_template<100>>().pretty_name() << std::endl;
/*
Result:
	std::tuple<number<1ul>, number<2ul>, Fizz, number<4ul>, Buzz, Fizz, number<7ul>, number<8ul>, Fizz, Buzz, number<11ul>, Fizz, number<13ul>, number<14ul>, FizzBuzz, number<16ul>, number<17ul>, Fizz, number<19ul>, Buzz, Fizz, number<22ul>, number<23ul>, Fizz, Buzz, number<26ul>, Fizz, number<28ul>, number<29ul>, FizzBuzz, number<31ul>, number<32ul>, Fizz, number<34ul>, Buzz, Fizz, number<37ul>, number<38ul>, Fizz, Buzz, number<41ul>, Fizz, number<43ul>, number<44ul>, FizzBuzz, number<46ul>, number<47ul>, Fizz, number<49ul>, Buzz, Fizz, number<52ul>, number<53ul>, Fizz, Buzz, number<56ul>, Fizz, number<58ul>, number<59ul>, FizzBuzz, number<61ul>, number<62ul>, Fizz, number<64ul>, Buzz, Fizz, number<67ul>, number<68ul>, Fizz, Buzz, number<71ul>, Fizz, number<73ul>, number<74ul>, FizzBuzz, number<76ul>, number<77ul>, Fizz, number<79ul>, Buzz, Fizz, number<82ul>, number<83ul>, Fizz, Buzz, number<86ul>, Fizz, number<88ul>, number<89ul>, FizzBuzz, number<91ul>, number<92ul>, Fizz, number<94ul>, Buzz, Fizz, number<97ul>, number<98ul>, Fizz, Buzz>
*/
}

Benefits and drawbacks of template metaprogramming

[edit]

Compile-time versus execution-time tradeoffs get visible if a great deal of template metaprogramming is used.

  • Template metaprogramming allows the programmer to focus on architecture and delegate to the compiler the generation of any implementation required by client code. Thus, template metaprogramming can accomplish truly generic code, facilitating code minimization and better maintainability[citation needed].
  • With respect to C++ prior to version C++11, the syntax and idioms of template metaprogramming were esoteric compared to conventional C++ programming, and template metaprograms could be very difficult to understand.[7][8] But from C++11 onward the syntax for value computation metaprogramming becomes more and more akin to "normal" C++, with less and less readability penalty.

See also

[edit]

References

[edit]
  1. ^ Scott Meyers (12 May 2005). Effective C++: 55 Specific Ways to Improve Your Programs and Designs. Pearson Education. ISBN 978-0-13-270206-5.
  2. ^ See History of TMP on Wikibooks
  3. ^ Veldhuizen, Todd L. (2003). "C++ Templates are Turing Complete". CiteSeerX 10.1.1.14.3670.
  4. ^ "Constexpr - Generalized Constant Expressions in C++11 - Cprogramming.com". www.cprogramming.com.
  5. ^ "Iterator Facade - 1.79.0".
  6. ^ "Constraints and concepts (since C++20) - cppreference.com". en.cppreference.com.
  7. ^ Czarnecki, K.; O'Donnell, J.; Striegnitz, J.; Taha, Walid Mohamed (2004). "DSL implementation in metaocaml, template haskell, and C++" (PDF). University of Waterloo, University of Glasgow, Research Centre Julich, Rice University. C++ Template Metaprogramming suffers from a number of limitations, including portability problems due to compiler limitations (although this has significantly improved in the last few years), lack of debugging support or IO during template instantiation, long compilation times, long and incomprehensible errors, poor readability of the code, and poor error reporting.
  8. ^ Sheard, Tim; Jones, Simon Peyton (2002). "Template Meta-programming for Haskell" (PDF). ACM 1-58113-415-0/01/0009. Robinson's provocative paper identifies C++ templates as a major, albeit accidental, success of the C++ language design. Despite the extremely baroque nature of template meta-programming, templates are used in fascinating ways that extend beyond the wildest dreams of the language designers. Perhaps surprisingly, in view of the fact that templates are functional programs, functional programmers have been slow to capitalize on C++'s success
[edit]