Thoughts on custom types

Source: Internet
Author: User

Recently, I saw a statement in the group: typedef typename T: value_type _ type. In my first response, this is a custom type. Yes, it is indeed a custom type, however, this is different from the custom type we usually see. T: value_type makes most people confused. In fact, the meaning is: This T must contain the value_type Member, that is, T must be a class or namespace. If you still do not understand it, continue to look down.

[Cpp]
// Custom type 1-this is the deformation of the conventional custom type. The imported classType should be a class and must have the value_type member, for example, the class in STL.
Template <class classType, class keyType = int, class valueType = char *>
Struct CCustomType
{
Typedef typename classType: value_type custom_type;
Custom_type data;
};

Use CCustomType to customize the type:
[Cpp]
CCustomType <string> string _;
String _. data = 'C'; // string: value_type is of the char type, which can be seen from the following basic_string
 
CCustomType <basic_string <char, char_traits <char>, allocator <char> string_2;
String_2.data = 'X'; // char
 
CCustomType <list <char *> list _;
List _. data = "you are so beautiful"; // char *
 
CCustomType <set <UINT> set _;
Set_. data = 512139461; // UINT

Declare CCustomType <map <int, char *>, int, char *> pair _;
Here pair _. data is the map member value_type, which is the pair type. More specifically, it is pair <int, char *> #
Before using CCustomType <map <int, char *>, int, char *>, let's take a look at the usage of pair in stl:
[Cpp]
Std: pair <int, char *> pair_1 (2012, "hello world"); // use the constructor to initialize pair_1
//
Std: pair <int, char *> pair_2;
Pair_2 = make_pair (1989, (char *) "hansen"); // OK make_pair value assignment
//
Pair_2.first = 2000; // OK
Pair_2.second = "just test"; // OK
//
Map <int, char *> nMap;
NMap. insert (pair_1 );
NMap. insert (pair_2 );
 
CCustomType <map <int, char *>, int, char *> pair _; // OK map should be used in this way. The key type is int type, and the value type is char *.
// (Pair _. data). first = 2012; error C3892: "pair _": the constant cannot be assigned.
// View pair _. data. first is why the const int type is changed to const int. I passed in the <int, char *> type, which should be the pair <int, char *> type, at first, I had this question.
(Pair _. data). second = "that's OK"; // OK, value is not const, and can be assigned a value

Analysis:
[Cpp]
// Declare a pair type variable pair_test according to the prototype above
Map <int, char * >:: value_type pair_test (2013, "test ");
 
// Follow up step by step through the source code to view the map's value_type, first: const int, second: char *
// 1. map value_type comes from _ Mybase member value_type:
Typedef typename _ Mybase: value_type;
// 2. Continue with the discovery that _ Mybase is a template class constructed by _ Tmap_traits:
Typedef _ Tree <_ Tmap_traits <_ Kty, _ Ty, _ Pr, _ Alloc, false> _ Mybase;
// 3. The answer is coming soon. Follow _ Tmap_traits to find that he has defined the pair type:
Typedef pair <const _ Kty, _ Ty> value_type;
// 4. This is why the map key is of the const type. It also indicates that the data member of the custom type CCustomType above is actually a pair <const T, T>

So how can we assign values to data members of the custom type CCustomType? The answer is that through the constructor, we first perform the following deformation on CCustomType:
[Cpp]
Template <class classType, class keyType = int, class valueType = char *>
Struct CCustomTypeEx
{
Typedef typename classType: value_type custom_type;
Custom_type data;
CCustomTypeEx (){}
CCustomTypeEx (keyType _ key, valueType _ value)
: Data (_ key, _ value) // call the classType: pair Constructor
{
}
 
}; Www.2cto.com
[Cpp]
CCustomTypeEx <map <int, char *>, int, char *> pair_ex (1989, "hansen"); // OK

Conclusion: The question about CCustomType <map, int, char *> pair _ is, in fact, I stole the concept from the very beginning. The previous sentence # is not completely understood, dead identification pair _. data is a pair type. We firmly believe that pair can be assigned after construction. That's right, regular pair can indeed do this, but I forgot the key phrase typedef typename classType in CCustomType :: value_type custom_type; that is to say, this value_type must be a member of classType, So pair _. data should be of the map: value_type type, followed by the pair <const int, char *> type.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.