C ++ generic programming and technical discussion of restricted parameter types

Source: Internet
Author: User

C ++ generic programming and technical discussion of restricted parameter types Template OverviewGeneric is an important feature in C ++. It is said that object-oriented programming has been replaced in the C ++ community to become the main generic type of C ++. STL and boost libraries are widely used in generics. Generic is the template mechanism of c ++. The template can be seen as a derivative of the C ++ macro. Macro, which is equivalent to replacement in the text file. Before compiling, the C ++ compiler replaces all the places where macros are used with macro definitions. Modern languages such as Java,. net, and Ruby do not have macro syntax. Macros are another reason why programs become obscure! I think macro should be avoided in the program as much as possible! A template can also be seen as a template. Before compiling, the C ++ compiler creates the source code of the template and then compiles the source code into binary code. Template TechnologyTemplate Class description and definition, such as: Template <typename T> class manage {... All inline function implementations !}; Function template definition, such:
 template<typename SequenceT> 
      void trim(SequenceT &, const std::locale & = std::locale());
  Template Specialization Templated class features1) first define the base generic: Template <typename T> class manage {... All inline function implementations !}; 2) then define the special generic: # include the above base generic file template <> class manage <B> {... All inline function implementations !}; The special generic type must implement all the member functions and static members defined by the basic generic type. Specialization of member functions of template classesIf we want the special generic to inherit the vast majority of basic generic code. You only need to define the special member function! Add the special member function: Template <> void manage <B >:: sayhello (void) after the basic generic definition) {cout <"B" <this-> T <Endl ;}; this special function is a member function of the special template class. In fact, this is equivalent to implicitly defining the above special template class, and all the basic implementations use the basic generic template implementation! Partial/partial specialA template class has multiple generic parameters. One template parameter is made special: 1) The base generic model has multiple template parameters: # pragma once # include "cppunit/extensions/helpermacros. H "# include" B. H "# include <iostream> using namespace STD; Template <typename V, typename T> class manage {PRIVATE: T * t; public: Manage (void) {This-> T = new T () ;}; void sayhello (void) {cout <"management" <this-> T <Endl ;}; public: virtual ~ Manage (void) {};}; 2) Is there a custom or any type parameter # pragma once # include "manage. H "# include" B. H "# include <iostream>/* is equivalent to template <typename V, no> class manage <v, b> no corresponding existing type B */template <typename v> class manage <V, B> {PRIVATE: B * t; public: Manage (void) {This-> T = new B ();} virtual ~ Manage (void) {}; public: void sayhello (void) {STD: cout <"B class" <this-> T <STD :: endl ;};}; However, please note that the half-exclusive mode does not have the simple form of the member function in the specific mode !!! Template usage Classes using templates should be written in header files and published as source codeIn C ++ generic programming, all the code used for generic declaration or definition must be written directly. the header file H cannot be written in. CPP file, otherwise there will be many strange errors! Vc2005 does not yet support export keywords for separate compilation! The template class can only be written in one. h file. In addition, it cannot be placed in a DLL project. Because the template class cannot be exported! After the template class is exported, the template class can only be declared externally, and the object of the template class cannot be actually created! Otherwise, testmain. OBJ: Error lnk2019: unresolved external symbol "_ declspec (dllimport) Public: _ thiscall metadata: objectrefmanage <class Aclass >:: objectrefmanage <class Aclass> (void) "(_ imp _?? 0? $ Objectrefmanage @ vaclass @ net_sf_interfacecpp_core_lang Qae @ xz). This symbol is referenced in function _ main. Because the template class is not actually compiled into binary code. It's just a macro! The source code needs to be generated during compilation based on the usage of the customer's code and then converted into binary code. Therefore, as a macro, it should be in the. h file. Source code metadata should be shared to users. It generates source code based on the customer's usage. Therefore, it must be included in the final customer code! To use the template class, you must take it out separately and hand over the. h header file/source code to the user. You can use this template class only by using this header file as the source code in the project! // Ensure that the system is introduced only once # ifndef _ net_sf_interfacecpp_core_lang_objectrefmanage_h _ # pragma once # include ".. /net_sf_interfacecpp/iobject. H "// The following are all custom. header files to be introduced to CPP files // # include "configapp. H "# include ".. /net_sf_interfacecpp/object. H "# pragma comment (Lib ,".. // debug // net_sf_interfacecpp.lib ")/* used to manage the lifecycle of any class instance, so that it complies with the iobject Interface Template Class which must be defined in the header file net_sf_interfacecpp_api */namespace detail {template <typename T> class objectrefmanage: Public iobject {PRIVATE: iobject * piobject; T * PT; // copy constructor objectrefmanage (const objectrefmanage & that ); // reload equals operator objectrefmanage & operator = (const objectrefmanage & that); // void operator Delete (objectrefmanage * thisptr); Public: T * getobjectptrandaddref () {This-> addref (); return this-> PT ;}; T * getobjectptrnotaddref () {return this-> PT ;}; objectrefmanage (void) {// the current reference is t His-> piobject = new object (); this-> Pt = new T () ;}; long addref () {return this-> piobject-> addref ();}; long release () {long result = This-> piobject-> release (); If (result = 0) {Delete this-> pt; Delete this; return 0 ;}}; void setsingleton () {This-> piobject-> setsingleton () ;}; public: Virtual ~ Objectrefmanage (void) {};}// ensure that only the system is introduced once # DEFINE _ net_sf_interfacecpp_core_lang_objectrefmanage_h _ # endif DLL dependency template usage1) The template depends on our dll2) if our class needs to use this template, we need to create another dll-ext.dll, including this template, to indirectly include the core DLL. The template can be used inside the DLL, because you can directly create the source code and compile it into a DLL based on the internal template usage. However, if you release the template inside the DLL, this will not work! 3) This template header file and DLL must be provided at the same time to avoid errors when the template dependent DLL cannot be found! There is no limit on template parameters.Evaluate the knowledge of Using Generics in STL and boost. I found a problem. When using the template class, the programmer can specify any class and basic type. However, in fact, many template classes have requirements for the operations that can be provided by parameter types in the internal implementation of the Code. For example, operations such as>, <, = are meaningful. Or you need to be able to call a method. However, parameters are not restricted in the STL and boost libraries! In this way, if the client programmer uses the wrong parameter type, the program can still be compiled normally. An error is reported only when this code is run. Even because STL and boost prefer to use Operator overloading, even when running, there will be no errors, but the real logic is wrong. How can this problem be found? I had a cool breath! I opened the book "design and evolution of C ++ language" by BS, the father of C ++, and BS described the template! BS does not need to limit the parameter type of the template. The limitation on template parameters is biased by OOP programmers! Dizzy! C ++ is a static compiling language, not a dynamic object-oriented language such as Ruby, Python, and JavaScript. If you use an object of the Error Type During Ruby development and there is no error during execution, it will not be reported until you run the code. I have nothing to say. People use interpreted language and give up compilation check errors, but in exchange for the huge dynamic flexibility of the language. Something must be lost! I won't talk about it anymore! However, BS believes that C ++ should not limit the parameter type of the template. When an error occurs during the runtime, I cannot understand it! BS, so many c ++ programs are in danger because of your preference for templates! A method that limits the parameter types of a template by deriving. Form: Template <typename T> class compare {}; template <typename T: Compare> class vector {}; BS does not think this method should be used. This method is often used when templates are used in Java. For example:
Public MyClass<E extends String>{
……
}
However, BS thinks this method is not good. In addition, I cannot compile such code in vs2005. Indeed, this will increase the number of template classes. The method mentioned in the second BS is very ugly. Is to convert the implementation of each method to the type we need. In this way, an error is reported during compilation. The third method is to use the template's special, or it is called specialization. This is a recommended method for BS. In my opinion, we should also use template specialization to limit the parameter types of the template. Although BS proposed this syntax, it is not intended to limit the parameter type of the template. BS does not consider that the parameter type of the template should be limited. Paranoid Guy! Use the template special restriction template parameter typeAs a strong oo programmer, I will not allow arbitrary parameter types in my own C ++ programs like STL and boost to use my template class at will! BS's point of view, I cannot disagree! In my opinion, you can use the template to restrict the parameter types of the template. This method is the simplest and most effective. First, we define a basic model. Then, several overload methods are defined externally in the basic template class. Specify the required parameter type and execute these methods. You can also define special template classes independently. However, as we have already said above, the special template class is not as cost-effective as the member function of the special template class! Finally, a custom exception is thrown in the implementation of the basic model. In this way, if an error type is used, an exception is thrown, causing the system to stop running. Our customers can find the problem. Of course, during compilation, even if it is an incorrect type, it can still be compiled. The error is caught only during running. No errors can be found during compilation. This is only because BS and C ++ standards committees do not provide us with syntax to limit the parameter types of templates. Supplement: differences between C ++ templates and Java templatesGeneric syntax also introduced in Java 5. For example:
Public MyClass<E extends String>{
……
}
It looks similar, but the actual implementation is quite different. C ++ templates will be compiled into many new C ++ classes. Therefore, there is a problem in using templates in C ++, that is, the source code generated by the template may be too many. Causes compilation performance problems. Java templates have different implementation mechanisms. The Java template class is erased during compilation. The source code of the new Java class is not generated. Because the class inheritance system of Java is single, and all classes are subclasses of the object class. Therefore, before Java 5, Java and Its set implementation classes were also very moist before the template syntax was introduced. When compiling a Java template class, I guess it is like this: 1. First, erase the information of the template type, or use the original object type. 2. Add forced type conversion to all the parameter types using the template to convert them to the template parameter types specified by the programmer. I especially remember BS's sentence, which makes sense: In C ++, he especially designed the syntax that should not be used ugly, so that you don't want to use it. For example: Dynamic_cast < Type-ID > ( Expression )Dynamic type conversion. BS believes that explicit type conversion is usually unnecessary. It should be avoided. I deeply agree with this sentence. Java introduces the template for this reason. Currently, writing Java code can be less forced type conversion! The Java compiler reports errors accurately based on the parameter type of the error template. Alas, it would be nice to use the C ++ template as well!

 

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.