The colon and his students (serial 10)--Super Paradigm

Source: Internet
Author: User

Super Paradigm

Intelligent Reproduction: Robot production Robot--preface

Quotation mark suddenly remembered one thing, asked: "Has a book called" C + + template Meta Programming "books, since mentions the template, wants to also belong to the generic programming? ”

The colon replied: "Template metaprogramming is template metaprogramming, closely related to GP, but is subordinate to another programming paradigm--metaprogramming (metaprogramming), referred to as MP. Here the prefix ' meta-' often translated ' yuan ', in fact, is ' super ', ' the line ' means. For example, metadata (Metadata) is data about the data, the Meta object (MetaObject) is the object, and so on, metaprogramming is naturally about the program, or write, manipulate the program. ”

The exclamation mark frowned: "It sounds a little round." ”

The colon projects another piece of code--

C + + (Meta programming):

template <int N>
struct factorial
{
   enum { value = N * factorial<N - 1>::value };
};
template <>      // 特化(specialization)
struct factorial<0>  // 递归中止
{
   enum { value = 1 };
};
void main()
{
   cout << factorial<5>::value << endl; // 等价于cout << 120 << endl;
}

The above uses the template element programming to realize the factorial operation. "The colon explains," is fundamentally different from the factorial implementations of the previous three basic paradigms: the value of factorial is computed at compile time and not at runtime. In other words, this code generates new code in the form of a template and is executed during compilation. ”

The exclamation mark puzzled: "What does this explain?" ”

The colon does not answer directly: "Suppose you need to batch process user documents, their format structure is given in advance, but neither as simple as CSV (comma-delimited), nor as standard as XML, and users may change the format standard at any time, how can I design this program?" ”

Exclamation mark a little thought, then replied: "Three modules: readers read input documents, the parser in accordance with the format standard to resolve, the processor to resolve the results of processing." ”

"Obviously the key is in the parser, if you're starting from scratch, then the problem is at least four." "Colon with the number of fingers:" First, time-consuming write parser code; second, time-consuming debugging parser code; Third, if the user changes the format standard, you have to repeat two things; four, if this program is part of a large program, any changes can mean software recompilation, connectivity, testing, packaging, deployment, and so on. If the company has to release patches frequently because of you, your job is probably precarious. ”

Or the period is clever: since talking about metaprogramming, it is necessary to use metaprogramming, according to different format standards automatically generate the corresponding parser code. But--this law, though once and for all, seems to be a little tricky. ”

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.