STL Knowledge Preparation: 1. C ++ Keyword Typename

zhaozj2021-02-08  395

/ ************************************************** ****************************** * SGI * STL is the work of STL's parent Alexander Stepanov and STL Matt Austern et al., It is today * * is the most famous, the best STL implementation version, all source code and instructions documents can be used from www.sgi.com/stl/ * * * * is our best model. But well known, STL use A large number of complex C features, * * Plus the STL itself is complex and huge, making reading code itself a very difficult job. The following text * * Word is some of my experience and guess in learning STL I hope to help everyone, and I hope to get * * Get everyone's criticism and finger, so that we will increase our joint increase. * * Myan * ******************* *********************************************************** *********** /

In SGI * STL source code, TypeName This new C keyword can be used to use it everywhere. Many people who have learned C before, in fact, its conventional usage is simple: in the declaration template function or template At class, traditional ways: template generic_function () {// ........

You can also write Template ------------ generic_func () {// .............} The introduction of this keyword is mainly to avoid Class may give it confusion. Original TypenAme is so simple, but there is a uncommon usage of TypenAme in the STL source code. If you don't understand, you will have difficulties when you read the source code. Because I am currently I can't find a description of this question, so I have tried it, I got a guess, now you are as follows, please know the ax.

First see a SGI * STL source code, taken from STL_ITerator.h

1: template 2: inline insert_iterator <_Container> inserter (_Container & __x, _Iterator __i) 3: {4: typedef typename _Container :: iterator __iter; 5: return insert_iterator <_Container> (__ x, __iter ( __i)); 6:}

The puzzling part is in the fourth line. Please think about it before watching my explanation, I dare not guarantee the correctness and comprehensiveness of the explanation below.

Explanation: I think TypeName is: notify the compiler, the stupid thing behind TypenAme is a type, not something else.

Example: // Tpname.cpp # include #include // for typeid () Operator

Using namespace std;

Template struct cone {// default member is public type;};

Template // uses a template class as a template parameter, this is a very common struct ctwo {// Please pay attention to the following two rows // type {////// * 1 type_typename cone :: One_Value_type two_value_type ; // * 2}; // The above two templates are only defined two internal public types, but please note the second class CTWO's Two_Value_type type // Rely on the CONE's one_value_type, and the latter depends on the CONE template class. The parameter type incorporated during instantiation.

INT main () {typef ape; type> twoint_type; twoint_type :: two_value_type i; int J; if (typeid (i) == typeid (j)) // If i is an INT variable Cout << "Right!" << Endl; // Print Right Return;} // ~ TPNAME.CPP

The above examples were compiled with G 2.91 under Linux. The result was printed. But if the * 1 line of the note number is removed, the comment * 2 line, then compile the error, the compiler doesn't know about :: One_Value_type Why, usually. After the type in the template class parameter, it will only reveal the true body, but this CTWO class is biased to rely on an existing CONE template class, I hope to pre-guarantee ctwo :: two_value_type and cone :: one_value belongs to the same type, This is, please ask TypeName to go out, tell the compiler, the back :: One_Value_type is a name of the type already existing, so the compiler can work smoothly.

转载请注明原文地址:https://www.9cbs.com/read-282.html

New Post(0)