Item 48: Understanding Template meta-programming

Source: Internet
Author: User

Item 48:be aware of template metaprogramming.

template meta-programming (METAPROGRAMMING,TMP) is the use of templates to write C + + programs that run at compile time. template Metaprogram is a program written in C + + that runs in the compiler. When the program finishes running, its output will still compile normally.

C + + is not designed for template meta programming, but since the 90, the usefulness of template meta-programming has gradually been discovered by the world.

    • The many conveniences provided by template programming are difficult to implement in object-oriented programming;
    • The program's working hours are transferred from the runtime to the compile time, and errors can be discovered earlier and run more efficiently.
    • In the design mode, the specific design pattern can be realized based on the different strategies and the automatic combination.
Static type checking

The implementation of this is mentioned in item 47 std::advance :

Template<TypeName Itert, TypeName Distt>void Advance(Itert& ITER, Distt D) {  if (typeID(TypeName STD::iterator_traits<Itert: :iterator_category) ==    typeID(STD::Random_access_iterator_tag)){      ITER += D;  }  ...}List<int: :iterator it;Advance(it, Ten);

advance<list<int>::iterator, Int> " In this statement:

iter  +=  d  ;   

list<int>::iteratoris a bidirectional iterator that does not support += operations. Although the above statement will not execute, the compiler does not know this. This statement will still throw a type error at compile time.

Template meta-programming

TMP was later shown to be Turing complete, which means that TMP can be used to calculate any computable problem. You can declare variables, execute loops, write and Invoke functions, and so on. But its style of use is completely different from normal C + +.

Let's take a look at how a loop is executed in TMP:

Template<unsigned N>struct factorial{    enum{ value = N * factorial<N-1: :value };};Template<>struct factorial<0>{    enum{ value = 1 };};int Main(){    cout<<factorial<5: :value;}

This is a typical TMP example, and its lows are like "Hello World" in a common programming language.

Use of TMP

To better understand the importance of TMP, let's look at what TMP can do:

    1. Make sure the dimension is correct. In scientific calculations, the combination of dimensions should always remain correct. For example, a variable of "M" must be divided into a variable of "s" in order to get a velocity variable (its unit is "M/S"). When using TMP, the compiler can guarantee this. Because different dimensions are mapped to different types in TMP.
    2. Optimize matrix operations. For example, matrix multiplication, there is a technique in TMP that has an expression template , which can be used to remove temporary variables and merge loops at compile time. Can achieve better run-time efficiency.
    3. The implementation of the custom design pattern. Design patterns tend to be implemented in a variety of ways, and a TMP technology called Policy-based (policy-based design) can help you create a separate design strategy, which can be combined in any way, by design choices. Generate countless design pattern implementations.

This address: http://harttle.com/2015/09/16/effective-cpp-48.html


Item 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.