STL programming practice five: neo-shape type declaration

zhaozj2021-02-11  158

STL programming practice five: neo-shape type declaration

Email - ccplusplus@21cn.com

Foreword

STL is a research results in generic programming. In the generic design, the nest-like type, the nest, is a customary method, which uses TypedEF health. "STL programming practice four" we have made some simple introduction to the use of the health word. Here, you said that using TypeDef Guan Jian word in generic design for nest-like types. It is also a supplement to "STL programming practice four", please don't laugh. Welcome to the criticism.

text

Nest-like types are very complicated, in fact, very simple, take an example:

Template

Struct CType

{

TypeDef t value_type; // This is the nest type type declaration

}

Simple! However, it has an important status in generic programming, mainly used to obtain the original type of Template quotes, let's talk about less appropriate examples (Note: Don't say that the syntax is wrong, just means this example has No use, or if there is anything necessary to write, etc.).

Template

Struct CType

{

Typedef T value_type; // Nest-like type declaration

}

Template

Struct CType // Offset

{

Typedef T value_type; // Nest-like type declaration

}

......

Template

TypeName CType :: Value_Type

FUNC (T T)

{

......

}

......

INT * PI = New Int (10);

INT IRESULT = FUNC (pi);

This simple program uses a nest-like type, the letter FUNC hopes to return the original type of the T core (here Int), when the FUNC's template quotes are int * T-type, INT * Therefore, the function of FUNC cannot directly return to Type T. By returning CType :: Value_Type, you can get the effect you want, this uses Traits technology. When the quota is int *, CTYPE :: Value_Type is herein is Int.

Can you understand? I hope to help everyone, I don't understand or have a mistake, please let me know, thank you first!

2001-11-28 nights

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

New Post(0)