C++ template from mastery to schizophrenia

C++ template from mastery to schizophrenia

Click to follow asynchronous books and put on top of the official account

Every day I share with you good IT books, technical dry goods and workplace knowledge

By-Zhao Yan

Make an advertisement first. If you like this article, you can go to zhaoyan.website/blog to view similar C/C++ articles.

This is an article about writing software, but it is very hard. Please give an early warning, and girls should not read it!

All articles written in C++, if there is no source code, are playing rogue. Don t gossip, May the source be with you!

This is a classic textbook example. Introduce the inheritance and polymorphism of C++. The only thing that needs to be emphasized here is: for the functions LetAnimalTalk and vector va, we can imagine that they are customers. [face= ] Encapsulate changes to the back of the base class through inheritance, so that customers who use the base class interface do not need to change! [/face] For customers, no matter how the base class changes, you will not affect me. For example, if a manager dog joins the project team now, your LetAnimalTalk function does not need to be changed.

So far so good! Now look at what happened after the introduction of the template?

The basic application scenario is like this. For an animal, you can use a string to represent its ID. If you don't want the developer to have a string name, you can also use an integer to represent its ID. The entire program above, if you change main to the following, except for the cat, there will be some comments, but everything else is fine!

One of the most obvious drawbacks of the above template inheritance is that the syntax has become more bloated and complicated. For example, you can't directly reference the variables of the base class in the subclass. If you quote him, you must use the syntax Animal::id_. The reason behind it is that when the compiler compiles Cat, Animal does not exist at all! For specific details, you can see Reference 1 below.

Another problem is the efficiency of virtual functions. This kind of polymorphism occurs when the program is running, and is mainly called and distributed through the virtual function table. So there is a certain efficiency loss. Let's look at another example below. Since cats don't like integer ID names, we completely remove this feature here and focus on how to use templates to achieve polymorphism.

In this code, there are a few points to note:

1) Polymorphism no longer needs to use pointers, we can use references to support polymorphism.

2) In the function LetAnimalTalk, which talkImplement called by pa.talk() is determined at compile time. This is a static polymorphic technique. So there is no loss of efficiency.

3) But the function LetAnimalTalk must now be a template function, and we have also lost the ability to save Cat and Developer with vector at the same time. This is the loss of flexibility brought about by efficiency improvements!

4) This is the CRTP mode, see Reference 2 for more introduction . The translation is "Continue to ask yourself curiously!" This should be an obvious initial symptom of schizophrenia.

So far, we have introduced three examples. All follow a basic IS-A logical relationship. In other words, Cat is an Animal, and Developer is also an Animal. The following describes the logical relationship of the three IMPLEMENT-BY. The first example does not use inheritance at all.

1) In this process, the stencil parameters passed implementation at compile time to put to different talk Animal behavior class. This method is widely used in STL. Specific examples are like the map class in STL

Among them, std::less is similar to our SayMiao above.

2) Because the import is a certain behavior, it is not appropriate to call it Cat, so the name of the class is called SayMiao.

In order to realize the IMPLEMENT-BY relationship, we can also use private inheritance:

1) Have you been

class Animal: private T{
This syntax is amazing! It's okay, let's take it slow. First of all, private inheritance is not an IS-A relationship. It is the relationship of IMPLEMENT-BY. Regarding when to use private inheritance and when to use composition. Please see reference 3.

2) This method is Parameterised inheritance, and it is also a common design pattern. Please refer to Reference 4

OK, the final question is, since private inheritance is OK, can shared inheritance work? In the eyes of a divisive patient, nothing is impossible!

1) Policy-Based design which is mentioned in the Modern C ++ design. A small tip is: now there is no need to talk this function in Animal.

I have given a total of six programs above. Go to https://www.onlinegdb.com/and copy these six pieces of code in, and modify them according to your own understanding and questions. "It's always shallow on paper, and I absolutely know that this matter is going to work." This is actually Lu You's piece of advice to the majority of programmers. If you have enough patience, you can slowly and deeply understand that there are quite a lot of fun things here. Due to the chapter relationship (mainly I won't be able to expand it!) I won't say much.

The above six programs respectively represent six different grammatical methods, expressing the two most basic design patterns IS-A or IMPLEMENT-BY. First of all, there is no difference between advantages and disadvantages. In different scenarios, each has its own advantages and disadvantages. In addition, the template of C++ is completely different from traditional C++ programming. His syntax and the semantics he wants to express have a very obvious tendency to split, and it is very easy to split the traditional C++ programmer. The so-called paradigm is the biggest pit of C++, but it is not enough to talk about life without jumping into this pit!

If you don't understand it, forget it! You can say: "This person is crazy!" I have already admitted this in the title!


1) https://eli.thegreenplace.net/2012/02/06/dependent-name-lookup-for-c-templates

2) https://en.wikipedia.org/wiki/Curiously_recurring_template_pattern

3) https://isocpp.org/wiki/faq/private-inheritance#priv-inherit-vs-compos

4) https://blog.feabhas.com/2014/06/template-inheritance/

This article comes from asynchronous community, author: Zhao Yan, works "C ++ template from proficient to schizophrenia" unauthorized, prohibited reproduced.

Long press the QR code to follow us

Share IT good articles with you every day.

Reply to "Follow" in the background of " Asynchronous Books ", and you can get 2000 online video courses for free; refer friends to follow the prompts to get the book donation link, and get a free asynchronous e-reading book. Come and join us now!

Click to read the original text to see more

Read the original