Template and inheritance art-name template parameters, inherit the name template parameters
I. Naming template parameters many template technologies drag a long string of type parameters, but many parameters have reasonable default values. Template <typename Policy1 = DefaultPolicy1, typename Policy2 = DefaultPolicy2, typename Policy3 = DefaultPolicy3, typename Policy4 = DefaultPolicy4> class BreadSlicer {}; however, if you need to specify a non-default, you must also specify all the real parameters before it, even if these real parameters are consistent with the default parameters. BreadSlicer <default1_y1, default1_y2, Custom> is more efficient if we can implement something similar to BreadSlicer <Policy3 = Custom>. The idea is as follows: (1) assign a parameter to something called Policy3 for Management. The above "=" can be changed to a template, and pass Custom as the real parameter of the template. Here we change Policy3 to Policy3_is, and use Policy3_is <Custom> to assign values. (2) Merge the default real parameters and modify the index with the new name. Class DeafultPolicies {public: typedef default1_y1 P1; // default1_y1 is a specific class. P1 is used to index typedef default1_y2 P2; typedef defapolicpolicy3 P3; typedef defapolicy4 P4 ;}; create a class similar to Policy3_is for management. Template <typename Policy> class policypolicis:VirtualPublic defapolicpolicies {// Why do I need virtual inheritance? Typedef Policy P1 will be revealed soon; // The P1 will be assigned a new value}; template <typename Policy> class Policy2_is: virtual public defapolicpolicies {typedef Policy P2 ;};... // The remaining Policy3_is and Policy4_is are similar to the preceding four classes.Modifier. How to install and install these modifiers? Create a "socket" with four "slots ".Before that, we should first solve a problem. What are the default parameters? According to the above mode, the default parameters should also be inherited from DefaultPolicies. Class DefaultPolicyArgs: virtual public defapolicpolicies {}; // default real ParameterThe "slot" should be occupied by default real parameters before inserting the "modifier". Therefore, it is a little tricky to inherit four identical types (default real parameters) into a class at the same time. Template <typename Base, int D> // use D to program different types of the same type, but the nature of the class remains unchanged.Class D: public Base {};Template <typename T1, typename T2, typename T3, typename T4>Class PolicySelector: public D <T1, 1>, public D <T2, 2>, public D <T3, 3>, public D <T4, 4> {};Multiple inheritance is required here. To avoid ambiguity, the previous "modifier" and default parameters both need virtual inheritance.Finally, assemble the default parameters. Template <typename PolicySetter1 =DefaultPolicyArgs, Typename PolicySetter2 =DefaultPolicyArgs, Typename PolicySetter3 =DefaultPolicyArgs,Typename PolicySetter4 =DefaultPolicyArgs> Class BreadSlicer {typedef PolicySelector <PolicySetter1, PolicySetter2, PolicySetter3, PolicySetter4> Policies; // use Policies: P1 to index the first real parameter .};BreadSlicer <Policy3_is <Custom> bc;The following is a complete example.# Include <iostream> # include <list> using namespace std; class DefaultPolicy1 {}; class defapolicpolicy2 {}; class DefaultPolicy3 {public: void print () {cout <"I am the default parameter 3" <endl ;}}; class DefaultPolicy4 {}; class DefaultPolicies {// combine the default real parameters with public: typedef defapolicpolicy1 P1; typedef default1_y2 P2; typedef DefaultPolicy3 P3; typedef defapolicpolicy4 P4 ;}; class defapolicpolicyargs: virtual public DefaultPo Licies {}; template <typename Policy> class policypolicis: virtual public DefaultPolicies {public: typedef Policy P1 ;}; template <typename Policy> class Policy2_is: virtual public DefaultPolicies {public: typedef Policy P2 ;}; template <typename Policy> class Policy3_is: virtual public DefaultPolicies {public: typedef Policy P3 ;}; template <typename Policy> class Policy4_is: virtual public DefaultPolicies {Public: typedef Policy P4 ;}; template <typename Base, int d> class D: public Base {}; template <typename B1, typename B2, typename B3, typename B4> class Selector: public D <B1, 1>, public D <B2, 2>, public D <B3, 3>, public D <B4, 4> {}; template <typename T1 = defapolicpolicyargs, typename T2 = defapolicpolicyargs, typename T3 = DefaultPolicyArgs, typename T4 = defapolicpolicyargs> class BreadSlicer {typedef Selector <T1, T2, T3, T4> Policies; public: void print () {typename Policies: P3 p3; // example of using one of the parameter types, // use the third parameter to display p3.print () ;}}; class Print3 {public: void print () {cout <"I am a customer parameter 3" <endl ;}; int main () {// BreadSlicer <> B; BreadSlicer <Policy3_is <Print3> B; b. print (); return 0;} EDIT: Claruarius. For more information, see the source.