Objective C ++ Clause 48: understanding of template-based programming

Source: Internet
Author: User
Tags traits

Template metaprogramming is the process of compiling a template-Based C ++ program and executing it during the compilation period. It is a program written in C ++ and executed in the C ++ compiler. Once the execution of the TMP program ends, its output, that is, some C ++ source code generated by the template, will be compiled as usual.

TMP has two great effects. First, it makes some things easier. Without it, those things will be difficult or even impossible. Second, since TMP is executed in the C ++ compilation phase, you can transfer the work from the runtime to the compilation phase. As a result, some errors can be detected at runtime, which can be found during compilation. The other result is that the tmp c ++ program may be more efficient in every aspect: smaller executable files, shorter runtime periods, and fewer memory requirements. Compilation takes a long time.

Article 47 states that the typeid-based advance solution is less efficient than the traits solution because in this solution, 1. type tests are sent at runtime rather than the compiler, 2. The runtime type test code will appear in the executable file. The traits solution is TMP, which triggers "If occurs in the type during the compilation period... Else computing.

The typeid-based implementation method of advance may cause compilation problems:

STD: List <int >:: iterator ITER;
Advance (ITER, 10); // compilation fails

This version of advance is generated for the above call:

Void advance (STD: List <int >:: iterator & ITER, int D)
{
If (typeid (STD: iterator_traits <STD: List <int >:: iterator >:: iterator_category)
= Typeid (STD: random_access_iterator_tag ))
{
ITER + = D; // wrong!
}
Else
...
}

List <int>: The iterator is bidirectional and does not support + =. We know that the row + = will never be executed, but the compiler must ensure that all source code is valid, even if it is not executed.

For the boost's MPL designed for TMP, refer to clause 55 to provide a higher level of syntax.

Let's look at the loop. tmp is done by recursion. TMP recursion does not involve recursive function calls, but involves the recursive template instantiation ).

Template <unsigned n>
Struct factorial {// General situation
Enum {value = N * factorial <n-1 >:: value };
};

Template <>
Struct factorial <0 >{// Special Case
Enum {value = 1 };
};

Like good recursion, we need a special case to end the recursion. The special case here is the template special object factorial <0>.

STD: cout <factorial <5>: value; // print 120

TMP is worth learning. There are three examples:

1. Make sure that the measurement unit is correct. With TMP, you can ensure that the combination of all measurement units in the Program (during compilation) is correct and can be used for early error detection.

2. Optimized matrix operations.

Bigmatrix M1, M2, M3, M4, M5;
Bigmatrix result = m1 * m2 * M3 * M4 * M5;

For normal function call calculation results, four temporary matrices are created, each of which is used to store operator * call results. Independent multiplication generates four loops acting on matrix elements. If the TMP-related template technology is used, that is, the so-called expression templates, it is possible to eliminate those temporary objects and merge the loop, which is not required to change the client approach. TMP uses less memory, and the execution speed is significantly improved.

3. You can generate customized design model implementation products. The design patterns can be implemented in multiple ways. Using the so-called policy-based design TMP-based technology, some templates may be generated to express independent design options (IES), and then they can be freely combined, this leads to the behaviors of model implementation products customized with customers. This technology has surpassed the Public Interest field of programming and has become a basis for Generative programming in a broader sense.

TMP may never become the mainstream, but for some programmers -- especially Library developers -- almost certainly become their main food.

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.