- Understanding implicit interfaces and compile-time polymorphism
- Multi-state and run-time polymorphism at compile time
- Run-time polymorphism is like when the virtual function is run again to determine which function the virtual function is bound to, and the function type is determined when it is run.
- Compile-time polymorphism is like generic programming and template programming, when compiled to determine which function should be called, depending on the type of function.
- The
- display interface and implicit interface
- Both classes and templates support interfaces and polymorphism, and for classes, interfaces are displayed, one centered on function signatures. Polymorphism is through the virtual function in the run time, for the template parameter, the interface is implicit, based on an effective expression. Polymorphism is through template with the initialization and function overload resolution occurs at compile time.
- The meaning of TypeName
- In the template declaration, class and TypeName are not different.
Template <classclass <typename t>class widget;
- Subordinate name and predicate non-subordinate name
- Subordinate name (Dependent typename): Indicates that the variable name is dependent on the template parameter, as determined by the profile parameter.
- Using this subordinate name may sometimes occur when the name is obscure, or the name is too long to cause the name to be unresolved. At this point we can use the TypeName keyword, which allows the compiler to know that a long name is a parameter type, which eliminates ambiguity.
- In general, we have a subordinate type name in the template, we need to add the keyword typename before it.
- Special:TypeName cannot appear in the base classes list before the name of the subordinate type, nor can it be used as the base class modifier in the member initialization list.
- predicate non-subordinate name (Non-dependent typename): This variable is not dependent on the template parameter, and is a normal variable name.
- typedef: The role is to replace a longer name with a short name
typedef typename Std::iterator_traits<itrat>:: Value_type value_type; // Here TypeName is the specified subordinate name, typedef is to make this long subordinate name into a short value_type.
- Learn to handle names within a templated base class
- The name of a member within a base class templates can be referred to in a derived class template by this->, or by a clearly written base class qualifier modifier
- Extract the parameter-independent code from the templates
- Templates generates multiple classes and functions, so any template code should not have a dependency on a template parameter that causes bloat.
- Code bloat caused by non-type last-class parameters can often be eliminated by replacing the template parameter with a function parameter or class member variable.
- Code bloat due to type parameters can often be reduced by allowing the existing type to share the implementation code with the same binary representation.
- Use member function templates to receive all compatible types
Reading notes--template and generic programming