Generic Programming (Templates)

Source: Internet
Author: User

; generic programming

that is the meaning of a wide range of types of programming, often used for those functions to repeat, but only the parameter type or return value is different, commonly used generic programming solution, C language with macro solution C + + implementation of generic programming with templeate template to implement, in fact, the code is copied, when the compiler scan to the template initialization syntax will be in the current file to instantiate a function or class into this file, the instantiated template is the existence of entities so can be debugged, and the template does not exist entities, No entities exist after a macro is replaced function Templates (write only the implementation of the function without concern for its type, the type will initialize a function template or class template by adding a parameter called type when it is initialized externally), Definition: Each function implementation, just the parameter type is not the same then write-only implementation, the rest of the compiler to help you complete the final code (before the compilation, after preprocessing)
when the compiler checks the syntax of the instantiated template1 Instantiate the template (that is, generate the corresponding class or function to the file based on the type) (note that this is the C + + compiler self-generated entity of the OH C is self-generated manually)2 do a grammar check3 Calls; C Language Macro Templates1 Define first2 Instantiate each type of template entity on its own (note that it's your own manual OH)a member function implementation in a class template cannot be placed separately in the CPP and must be placed in. h and declared together (the inline function is the same)The reason is that the processing of the template will not process the. cpp file after preprocessing, in other words, if the implementation is not found, the error occurs because the CPP file was not processed at that time. if the type of template is passed in a different template, you need to add a space .A special case of a class member function (which passes a specific argument, then this member function of the class will use a specific function to instantiate the class through the template) class Exception (the specified type is passed the constructor or destructor, the member function all come here oh (the entire class of code is particularly instantiated) this is the class special case) data member exception (only static verse members are exceptions) 
The class template passes another class template, that is, when the instantiation of a class in a nested relationship, and then the outside of the template generated by the class, using this class to do the type of parameters, or what the   

Generic Programming (Templates)

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.