Introduction to template metaprogramming

Source: Internet
Author: User

Quote:

When we need to use a constant to be calculated in the program, but do not want to write expressions in the Program (because it affects the program speed ), what should I do if I don't want to use a calculator to calculate the result first? A: define or const can be used. For example, if I want to use how many seconds a day, I will define a macro (# define day_sec 24*60*60) or define a constant (const unsigned day_sec = 24*60*60 ;), in this way, I can directly use day_sec or day_sec in the program, and the compiler will calculate it for me in advance. However, I want to use the nth constant of the Fibonacci series, and I want to use the square root constant of the integer n... what should I do? A: You can use TMP.

Refer:

#include <iostream>using namespace std;template<unsigned n>struct Factorial {  enum {value = n * Factorial<n-1>::value};};template<>struct Factorial<0> {  enum {value = 1}; };int main() {  cout << Factorial<10>::value << endl;  return 0;}

Explanation:

History:

Erwin Unruh presented a special piece of code during a C ++ Standards Committee meeting held in San Diego on 1994, during the compilation phase, all prime numbers from 2 to a given value can be generated by compiling error messages. In the summer of the same year, Todd veldhuizen was inspired by Erwin's example and found that he could use the C ++ template for metaprogramming and published a technical report. In May of the following year, I published an article named "using C ++ template metaprograms" in C ++ report, in this way, the C ++ compiler template programming (compile-time) discovered by Erwin Unruh
Template programming) further refined to C ++ template meta programming (template metaprogramming, TMP ).

Thoughts:

The template special mechanism is used to implement the conditional selection structure during the compilation period, and the recursive template is used to implement the cyclic structure during the compilation period. The template meta program is interpreted and executed by the compiler during the compilation period.

Template meta-programming uses static C ++ language components. The programming style is similarFunctional ProgrammingWhere variables, value assignment statements, and iteration structures cannot be used. In Template metaprogramming, it mainly operates on constants and types of integer types (including Boolean, character, and integer.The manipulated object is also called metadata (metadata). All metadata can be used as template parameters. Because variables cannot be used in template metaprogramming, we can only use typedef names and Integer constants. They are initialized using a type and an integer respectively, and cannot be assigned a new type or value. If a new type or value is required, a new typedef name or constant must be introduced.

Resource:

[1] Todd veldhuizen, templatemetaprograms, http://osl.iu.edu /~ Tveldhui/papers/template-metaprograms/meta-art.html

[2] Todd veldhuizen, expression templates, http://osl.iu.edu /~ Tveldhui/papers/expression-templates/exprtmpl.html

[3] Todd veldhuizen, C ++ templates as partial evaluation, http://osl.iu.edu /~ Tveldhui/papers/pepm99 /.

[4] Todd veldhuizen, scientific computing: C ++ versusfortran, http://osl.iu.edu /~ Tveldhui/papers/drdobbs2/drdobbs2.html

[5] Erwin Unruh, prime numbers (primzahlen-original), http://www.erwin-unruh.de/primorig.html.

[6] Erwin Unruh, prime numbers (primzahlen), http://www.erwin-unruh.de/Prim.html.

[7] Edward Rosten, floating point Arithmetic in C ++ templateshttp: // mi. Eng. Cam. ac. uk /~ Er258/code/fp_template.html.

Part reference: http://blacfun.blog.163.com/blog/static/2484748120084173259314/

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.