Elaborate C + + template meta-programming

Source: Internet
Author: User

Template meta-programming root in templates, the use of templates is simple: To facilitate automatic code generation, a very effective way to improve programmer productivity is code reuse, and object-oriented is a very important contribution is to transform ideas into reusable concepts through internal tight coupling and external loose coupling, But the object-oriented toolkit contains the inherited, combinatorial, and polymorphic features that do not fully meet the overall requirements for code reuse in real-world programming, and the city template emerges.

Template when smarter macros, templates and red are generated before compiling code, template and red compared to it, it stands on higher abstraction above, the macro operation is the string token, but the template can operate in C + + type, so the template is more secure, more intelligent

Finish the template, and then template meta-programming, template meta-programming is actually a complex point template, the simple template in the special when the basic only contains the type of search and replace, this template can be regarded as a type of security macros, and template meta-programming is a few commonly programmed concepts such as: recursion, Branches and other templates in the process of adding template, but in fact, the template, automatic code generation.

The basic principle of template metaprogramming (TMP) for C + + is to transfer the load from runtime to compile time while maintaining the original level of abstraction. The load can be divided into two categories, one is the cost of the program running itself, a class of programmers need to write code. The former can be understood as compile-time optimization, while the latter is to improve the code reusability, thus improving programmer's programming efficiency.

If we use float** to represent a multi-dimensional signal, then if we are going to copy two of these multi-channel signals,
In C + +, we generally write this:

float** in; float** out;for (size_t ch=0; ch<channelnum; ++ch) {for    (size_t i=0; i<length; ++i) {        out[ch][i]=in[ch ][i];}    }

It's a standard thing to write about, but, as a performance freak, I'm not comfortable with that.
First, if at compile time I can be sure that channelnum is a definite value, say 4, then the above code can be optimized to:

for (size_t i=0;i<length;++i) {    out[0][i]=in[0][i];    Out[1][i]=in[1][i];    Out[2][i]=in[2][i];    Out[3][i]=in[3][i];}

The advantage of this is that there are at least two, 1. Optimized for the branch, 2. Optimized for data cache refresh. It's got a performance boost, but the code is written dead, Channelnum must be 4, what if I need 3, or 5? This is where the template element comes in handy:

   Template<int count>class copy{public:        static inline Go (float* const out, float* const, int i) {    copy< Count-1>::go (out,in,i); out[count][i-1]=in[count][i];}; Template<>class copy<0>{public    :     static inline Go (float* const,float* const,int) {}};

  

This way, no matter how large the cout parameter is, go must run Copy<cout-1>::go again whenever you run the "Go" function.
This is a recursive structure, and I use it to build a template class "Copy" that can generate code automatically, and then I write:

Template<int channelnum>void palll_copy (float** out,float** in,size_t length) {for    (size_t i=0;i<length; ++i) {        copy<channelnum>::go (out,in,i);    }}

In summary, the biggest advantage of template element is that when you need an extreme pursuit of performance, even the normal C + + code performance can not meet you,

Please use the assembler, but when you are both stronger in performance, with greater degrees of freedom and code reuse, when you try to squeeze the last little moral integrity of the compiler, when you try to abuse the compiler so it often pops up the template context too complex, when you are so idealistic, To make the compile time crazy 10 times times in exchange for user 0.1ms performance improvement, you can consider using template elements.

Elaborate C + + 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.