Valid C ++ Clause 48, valid Clause 48

Source: Internet
Author: User

Valid C ++ Clause 48, valid Clause 48
Terms of this section: Learn about template metaprogramming

The terms of this section are a brief introduction to template metaprogramming, so that readers can know that there is such a programming method, more specifically a technology.
So what is template metaprogramming? What are the benefits of template metaprogramming? According to the author's original statement:
1. TMP can transfer the work from the runtime to the compilation phase, so as to achieve early error detection or higher execution efficiency.
2. TMP can be used to generate custom code based on Policy Selection combinations. It can also be used to avoid generating code that is not suitable for some special types.
Let's take a look at the author's code.

template <int N>struct Factorial{    enum    {        value = N * Factorial<N - 1>::value    };};template <>struct Factorial<0>{    enum    {        value = 1    };};int main(){    cout << Factorial<5>::value << endl; }

From the code above, we can see that we have implemented a factorial function. Call a statementFactorial<5>::valueWe can get the results we need. The driving force of this Code is the template instantiation process during compilation. The final work is completed by the special-case template.

If we have a good understanding of Clause 47, we will be able to feel the advantages of template metaprogramming again. We can identify types during compilation and allocate functions during compilation.
We know that Article 47 can also be implemented through the typeid method, but sometimes the typeid method may fail to be compiled, as shown in the following code:

template<typename Iter, typename DistT>    void advance(IteT& iter,DistT d)    {        if(typeid(typename std::iterator_traits<IterT>::iterator_category)        ==typeid(std::random_access_iterator_tag))            iter+=d;        else        {            if(d>=0)                while(d--) ++iter;            else                 while(d++) --iter;        }    }

If we call the statementstd::list<int>::iterator iter; advance(iter,10);Instantiate the advance function. The instantiation process is as follows:

Void advance (std: list <int >:: iterator & iter, int d) {if (typeid (typenamestd: iterator_traits <std: list <int> :: iterator >:: iterator_category) = typeid (std: random_access_iterator_tag) iter + = d; // error else {if (d> = 0) while (d --) ++ iter; else while (d ++) -- iter ;}}

The preceding statementiter+=d;Compilation fails becauselist<int>::iteratorThe iterator_category type of is bidirectional, while bidirectional does not have the + = Operator operation, so the compilation fails. Although the if statement will not be executed in actual operation, the compiler needs to compile all statements during the compilation process.

For TMP programming, I have gained so much.

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.