Boost :: Iterator

zhaozj2021-02-16  49

Sender: huxw (Mi Duck and Tang Mouse), the letter area: Programming Title: Boost :: Iterator_adaptor (i) Send Station: BBS Shuimu Tsinghua Station (Sat May 18 23:27:33 2002)

There are many content, some of the part. Let's talk about the principle of Typelist implementation.

I) Type-list, type list. Very tricky method, the first time I saw it is in the Loki Rally. Sometimes we do need a type of list, you can easily traverse access. And the general container is not a type of What should I do? Look below.

First, how is the list of types here? It is very simple Template struct cons_type {type;};

In this case, cons_type :: first_type is int, and second_type is Double. And the wonderful role is the nest, cons_type , long> :: first_type is cons_type < INT, DOUBLE>, and first_type :: first_type is int. In this library, an additional end_of_list indicates the end of the class table. Do you understand? I understand that I will continue;)

Then introduce a Find_Param template, you can find a specific type from a type list. For compilers that do not support offset, the methods are as follows: Template struct find_param {typedef typefetame Find_Param_Helper1 : : type select1; typedef typename select1 :: template select :: type type;}; where template struct find_param_helper1 {typedef find_param_continue type;}; template <> struct find_param_helper1 {typedef find_param_end type ;}; and struct find_param_continue {template struct select {typedef typename AssocList :: first_type Head; typedef typename Head :: first_type Key1; typedef typename Head :: second_type Value; typedef typename if_true <(is_same :: Value> :: template then :: type

Then introduce a Find_Param template, you can find a specific type from a type list. For compilers that do not support offset, the methods are as follows: Template struct find_param {typedef typefetame Find_Param_Helper1 : : type select1; typedef typename select1 :: template select :: type type;}; where template struct find_param_helper1 {typedef find_param_continue type;}; template <> struct find_param_helper1 {typedef find_param_end type ;}; and struct find_param_continue {template struct select {typedef typename AssocList :: first_type Head; typedef typename Head :: first_type Key1; typedef typename Head :: second_type Value; typedef typename if_true <(is_same :: value)> :: template then :: type> :: type type;};}; struct find_param_end {template Struct Select {typedef detail :: default_argument Ty PE;};}; From the above code, when Key1 and Key2 are the same type, it is the time when the template is completed. Otherwise, the template does not stop nested, knows the right key2 (is_same) or encounter Up to END_OF_LIST (Template). If_true's template is in Detail / Select_Type.hpp, it is very clear.

Flower two, if it is a compiler that supports specialization, it is more simple.

Template struct find_param;

Template struct find_param {type;};

template struct find_param , Rest>, Key> {struct find_param {typedef default_argument type;};

Template struct find_param , rest>, key> {typedef value type;}; // is biased, indicating Key and Key case where the same template struct find_param , Rest>, Key2> {typedef typename find_param :: type type;}; // Otherwise, the template is nestled.

Typelist is now a common tip, or that the MODERN C Design is proposed, it is recommended to find more detailed descriptions, mainly to establish TyPelist.;)

- AbcdefghijklmnopqrStuvwxyzabcdefghijklmnopqrStuvwxyz

※ Source: · BBS Shuimu Tsinghua Station bbs.edu.cn · [from: 166.111.172.6]

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

New Post(0)