1. Understanding implicit interfaces and compile-time polymorphism
For the template parameter, the interface is implicit, based on the constraint of the valid expression
Compiler polymorphism: templates function with different template parameters causes different functions to be called
2. Understanding the dual meaning of TypeName
When we declare the template type parameter, the meaning of class and TypeName is exactly the same
The name that appears in the template, if dependent on a template parameter, is called a subordinate name, and if the subordinate name is nested within class, it is called a nested subordinate name. C + + rules: If the parser encounters a nested subordinate name in the template, it assumes that the name is not a type unless you tell it to.
Any time you want to have a nested subordinate type name in the template, you must put the keyword Typename,typename in the previous position next to it to identify the nested subordinate type name, and the other name should not have it.
Template <typename c>void F (const c& container, // do not allow use of TypeName // Be sure to use TypeName
The exception to the rule that TypeName must be prefixed with a nested subordinate type name is that TypeName cannot appear before the nested subordinate type name in the base class list or as the base class modifier in the member initialization list
Template <typename t>class Derived: public base<t>:: nested{ Public : Explicit Derived (int x) : Base<T>:: Nested (x) { typename Base<T> :: Nested temp; }};
Std::iterator_traits<itert>::value_type // type of object referred to by Itert
Examples of using TypeName in real programs:
Template <typename itert>void workwithiterator (Itert iter) { typename std::iterator_traits <itert>::value_type Temp (*iter);}
3. Learn to handle names within a templated base class
The base class template may be special, and the specific version may not provide the same interface as the generic template, so C + + often refuses to look for inherited names within the templated base class
Solution: The compiler promises that any specialization version of the base class template will support the interfaces provided by its generic (generalized) version
1) Add "This," before the base class function calls the action
2) Use using declarative
3) Make it clear that the function being called is in the base class and that the function is called with the name of the basis, which loses polymorphism
4. Extract the parameter-independent code from the template?
5. Use the member function template to accept all compatible types
There is no inherent relationship between the different forms of the same template, and if a template is present with the B and D two types with base-derived relationships, the two bodies produced will not have base-derived relationship with each other.
Template<typename t>class smartptr{public: template<typename U > // for any type T and any type U, you can generate a smartptr<t> smartptr based on smartptr<u> (const smartptr<u>& other );};
6. Define a non-member function for a template when a type conversion is required
When we write a class template that provides the "related to this template" function to support "implicit type conversion of all parameters," define those functions as friend functions inside the class template
7. Please use traits classes performance type information
Traits is not a C + + keyword or a pre-defined artifact, it is a technique and is a protocol that every C + + programmer adheres to, allowing you to obtain certain types of information at compile time , traits is always implemented as Struct,iterator_ Traits is the traits for iterators
The way iterator_traits works is to declare a typedef named Iterator_category within a struct iterator_traits<itert> for each type of Itert. For this it requires that each user-defined iterator type must be nested with a typedef named Iterator_category.
Iterator_traits provides a partial version of the pointer type specifically:
Template<typename itert>struct iterator_traits (itert*) { typedef random_access_iterator_tag Iterator_category;};
Summarize:
1) Traits classes makes the type-related information available in the compiler, which is implemented with templates and templates specificity
2) Traits classes may perform if...else tests on a type at compile time after consolidating the function overloading technique
"Effective C + +" template and generic programming