"Effective C + +" key Summary (vii)

Source: Internet
Author: User

"Effective C + +" chapter Seventh: template and generic programming
  1. Learn about implicit interfaces and compile-time polymorphism. Object-oriented programming always uses an explicit declaration of an interface, and provides a special implementation in subclasses, thus realizing the run-time polymorphism. The interfaces in a template class are often implicit, implicitly meaning that when writing a template, it is assumed that the required interface class T is present, and that the class that instantiates the template actually has that interface, which is not known at compile time (if the instantiated type does not implement these interfaces, the compilation fails). This is how template polymorphism is presented, and the class that instantiates the template determines what the specific behavior is. This polymorphic compilation period and the runtime polymorphism is very different, the runtime polymorphism is a is-a type of polymorphism (my self-made words, want to express this polymorphism is like a family of polymorphic), and the template polymorphic range is wider, the scope of the occurrence is not limited to the family system, can be said to be arbitrary. The run-time polymorphism is mainly by virtual table and virtual function pointers, and template polymorphism is more like text substitution, and the runtime function entry point is deterministic.
  2. Understand the dual meaning of TypeName.
    1) When using the template parameter declaration, there is no difference from the class.
    2) Tell the compiler that the token after TypeName represents a type, when no other keywords can be substituted. The main purpose is to eliminate the ambiguity that is brought to the compiler. However, do not use him as the base class modifier in the base class list or Memberinitialization list.
  3. Learn to handle names within a templated base class. The base class is a template class, but what if you need to take a member of the base class in a subclass, you can't just take it as you used to, because the compiler doesn't allow it, and the compiler's lookup rules don't seem to work here. There are three ways of using base class Members:
    1) Add this-> before the members of the collection.
    2) using a using declaration in a subclass declaration, let the compiler perform the lookup rule he used before, formally and de-subclass the same as the base class function mask, but solve the problem differently.
    3) Use:: Explicitly indicate that the member is in the base class (this method suppresses the run-time polymorphism).
  4. Extract the parameter-independent code from the template. Do not put the type-independent code in the angle brackets behind the template, which can lead to scary code bloat, such as: Template (<) class T,int n> (the parentheses in this statement are unnecessary, Just no, the markdown editor doesn't show what's behind it. int n is the type-independent code, take it off! Replace with a member variable or function parameter (I think this is a very intuitive solution, do not know why the book is proposed, the template is originally for the type, against the variable is not appropriate)! In fact, even if there are only type parameters in the angle brackets behind the template, it can also cause code bloat, such as when the instantiated type is a variety of pointers (the solution I don't know what that means ...). The way to solve this problem is to get these t* (or T??? ) to manipulate another untyped pointer so that a function of the untyped pointer completes the actual operation.
  5. Use the member function template to accept all compatible types. Using two classes that have an inheritance relationship to instantiate a template gets two classes that do not have this inheritance, and the compiler thinks that they are two classes that cannot be converted to each other, and that in order to implement the transformation, the member function of the template class is also used as a template function, that is, if class-T is used in the classes declaration, Then when declaring its member function, declare a template return_type function_name (para_list) (the member functions of the Templates class are inherently template-like, but under the control of T, let these member functions be shown to support all types, You can be T), this technique if it works on the coping function, you also need to make a regular coping function (that is, the version controlled by T).
  6. Define a non-member function for the template when a type conversion is required. The fourth article in this series has a clause similar to this one, but for templates, things become less simple. Similarly, in the case of the operator of the plural class , if the compiler is to compile a mixed type call, it will have the type derivation ambiguity, that is, the derivation result of the type of the first parameter and the second parameter is inconsistent, and the compilation fails when the type conversion is not reached. The solution is to declare this operator function as a friend function (meaning the non-member function), and then implement the operator at declaration time, you can write the required calculation steps in the body of the function, or you can invoke another template function that really does the * operation. If you only declare a friend function in the template class declaration and implement it outside of the class declaration, the compilation is fine, but the link will go wrong, because the connector cannot find the implementation body of the declared function. Why can't I find it, I don't know.
  7. Please use traits classes to perform type information. If the template class wants to make certain judgments based on the specific type, thus compiling a more appropriate version of the class, then trait technology is the right candidate to make some judgments about the type at compile time, such as whether a type is a pointer or not. is a type a built-in type or a user-defined class? Instead of having to wait for the run time to use the typeID (typeID is not necessarily able to achieve the goal, because it requires a compile-time judgment). Trait means extraction--the extraction type of the relevant information. In order to achieve a compile-time type of judgment, there is also a need for a technology, called template-specific, template-biased and full-special points. The special meaning is that when you write a class template, you create a specific template for some special types, in addition to the generic version. It is generally done by making specific types of all or some template parameters. A small example: http://blog.csdn.net/liao_jian/article/details/45055887
  8. Recognize template meta-programming. Template meta-programming is a whole new world, not object-oriented or the usual so-called template so simple, it is more like functional programming, but basically all the calculations are completed in the compilation period, and C + +, C, Java and other sequential language is very different. The template element is "Turing complete", which means that the template element can do any calculation, with a large number of recursive, template-specific technology to do the calculation. However, the practice of template meta-programming is relatively small, relying mainly on personal experience. If you want to see, then you can look at the boost library or "C + + Design new thinking", it should be fruitful.

Effective C + + key Summary (vii)

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.