Effective C + + clause 48 understanding template meta-programming

Source: Internet
Author: User
Tags traits

1. Template meta-programming (MATAPROGRAMMING,TMP) is the process of writing a C + + program and executing it at compile time, "the so-called template Mataprogram (templated meta-Program), is written in C + +, executed in C + + The program within the compiler. Once the TMP program finishes executing, its output, that is, a number of C + + sources from templates, will be compiled as usual. "

2. Since the templates was introduced into the C++,TMP, the TMP has been proven to be very useful since TMP was discovered in the early 1990s. TMP has two great effects:

1). It makes things easier.

2). Template program's implementation of the C + + compilation period allows work to be transferred from the compile period to the execution period, which makes it possible to detect errors ahead of time and produce files with smaller executables, shorter runs, and less memory requirements, at the cost of longer compilation times.

The traits solution in clause 47 is TMP, because traits causes the if...else operation to occur on the type at compile time: replacing the If...else function that occurs during the execution period with the overloaded template function parameter matching behavior at compile time.

TMP has been proven to be "Turing complete", using TMP to declare variables, execute loops, write and Invoke functions ... A higher-level syntax is provided for the TMP-designed library (for example, Boost's MPL). " However, this component looks very different relative to the normal C + + counterpart, such as the TMP if-else conditional sentence shown in clause 47 is expressed by templates and its special body, and another example is the loop, where TMP is the loop that is completed by recursion:

The function of using TMP to compute a factorial is as follows.

template<unsigned n>struct  factorial{    enum{value=n*factiroal<n-1 >:: Value;};} Template<>struct factorial<0>{    enum{value=1 };}

As shown above, the recursion of TMP does not involve function calls, but recursive templates are present.

3. Important Applications of TMP:

1) Ensure that the unit of measurement is correct

2) optimize the matrix operation.

3) Create customized design pattern implementation products (such as strategy (terms of service), Observer,visitor, etc.).

TMP currently has its disadvantages: The syntax is not intuitive, the support tools are inadequate, and because TMP is a language found outside of a relatively short period of time, its programming needs to rely on experience ... However, the efficiency improvements that TMP brings are still noticeable, and C + + 's support for TMP is likely to be more and more.

Effective C + + clause 48 understanding template META programming

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.