When is it preferable to use template design based on templates without templates based on

I am trying to understand the real requirement of using templates for policy-based development. Looking through new C ++ template projects, I found that policy-based design is a highly recommended design method that allows you to β€œconnect” different behaviors from policy classes. A minimal example is the following (shortened version of the wiki):

template <typename LanguagePolicy> class HelloWorld : private LanguagePolicy { using LanguagePolicy::message; public: // Behaviour method void run() const { // policy methods cout << message(); } }; class LanguagePolicyA { protected: std::string message() const { return "Hello, World!"; } }; //usage HelloWorld<LanguagePolicyA> hello_worlda; hello_worlda.run(); // prints "Hello, World!" 

A quick analysis shows that just to get different smooth message() methods, we inherit a template type whose definition can be provided by anyone (and identified at compile time).

But the same level of abstraction (and custom methods) can be achieved without using boilerplate code and simple old-school old time polymorphism, as shown below.

 class HelloWorld { LanguagePolicy *lp; //list of all plugable class public: HelloWorld(LanguagePolicy *lpn) { lp = lpn; } // Behaviour method void run() const { // policy methods cout << lp->message(); } }; class LanguagePolicy { protected: virtual std::string message() const; }; class LanguagePolicyA: LanguagePolicy { protected: std::string message() const { return "Hello, World!"; } }; //usage HelloWorld helloworld(new LanguagePolicyA); helloworld.run(); 

Functionality and abstraction level wise I do not see much difference in the two approaches (although the second approach has several additional lines of code for LanguagePolicy , I think it is necessary for other users to know the interface, otherwise the understanding of LanguagePolicy depends on the documentation). But I think that later it will be "clean" (from someone who has not used the template a lot). This is because personally, in my opinion, non-template classes are clean to watch and understand. An extremely good example is the popular VTK library (a set of visualization tools), which solves many different problems using the second approach. Despite the fact that VTK does not have extensive documentation, most of us are its users, you can just look at the class diagrams (sometimes they are quite large) and display the behavior of the classes; and develop highly customizable and complex pipelines in our application (you cannot create VTK to create templates :)). The opposite is libraries such as STL / BOOST, which I believe may not be available so that anyone can identify how classes work without extensive documentation.

So my question is, is template-based design really superior (only in this policy-based development scenario), what is virtual inheritance based on? If so, when and why?

+6
source share
2 answers

Both are valid structuring methods, it really depends on the requirements. For instance.

Runtime and compile-time polymorphism.

When do you want / can / should achieve polymorphism?

Virtual Call Performance Overhead

Templates generate code that is not specified.

Actual use of the class.

When you need to store heterogeneous collections, you need a base class, so you need to use inheritance.

A very good book on policy-based development (a bit dated but good, nonetheless) Modern C ++ design

+1
source

It depends on the situation, I think ... A possible drawback of using templates is that the type must be known at compile time:

 HelloWorld<English> hw; // English is plugged at compile-time 

In your second example, where you use a pointer to a base, this pointer can point to many derived classes. What it indicates does not have to be known at compile time, and therefore can be determined by (user-) input at run time. A possible side to this approach is the virtual call overhead. On some applications and on some platforms this may not be desirable.

+1
source

Source: https://habr.com/ru/post/988388/


All Articles