Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Template metaprogramming
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
==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 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 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. Template metaprograms have no [[Immutable object|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 (computer science)|recursion]], as seen in the example below. ===Using template metaprogramming=== 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.
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)