Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Case Analysis of traits method of C++

2025-02-21 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)05/31 Report--

Today, the editor would like to share with you about C++ 's traits method case analysis related knowledge points, the content is detailed, the logic is clear, I believe most people still know too much about this knowledge, so share this article for your reference, I hope you can get something after reading this article, let's take a look at it.

To be clear, I am using the gcc7.1.0 compiler, and the source code of the standard library is also this version.

Let's take a look at the mind map first, as follows:

1. Pointer_traits description of pointer extractor

First of all, ha, there is no official name pointer extractor, in fact, pointer_traits is a class template, it was introduced after centering 11, you can get the corresponding pointer type through the incoming rebinding template type, the more official description is: the pointer_traits class template provides standardized methods for accessing some properties of the class pointer type.

So why take this pointer_traits out and explain it separately? because, like the previous memory allocator, it is a prerequisite for the use of some containers in stl. When talking about containers, you can't get around it, so figuring it out first is helpful for subsequent learning and understanding.

Why is it called pointer extractor? I understand that it is similar to memory extractor allocator_traits, which is based on template parameters to get a certain type, and traits also means extraction, so I call it pointer extractor here.

two。 Source code analysis of pointer extractor

The class template pointer_traits has two versions in the standard library, a specialized version and a non-specialized version, and the source code is all in the bits/ptr_ traits.h header file, of course, it is included in the header file memory when actually used.

2.1Despecialization pointer_traits

Let's first analyze the source code of the non-specialized version, as follows:

/ / pointer_traits class template template struct pointer_traits {private: template using _ _ element_type = typename _ Tp::element_type; template using _ _ difference_type = typename _ Tp::difference_type; template struct _ rebind: _ _ replace_first_arg {} / / if the type exists in the _ _ void_t parameter, use the following structure directly, otherwise use the above template struct _ rebind {using type = typename _ Tp::template rebind;}; public: using pointer = _ Ptr; using element_type = _ _ detected_or_t; using difference_type = _ _ detected_or_t Template using rebind = typename _ rebind::type; static_ Ptr pointer_to (_ _ make_not_void& _ e) {return _ Ptr::pointer_to (_ _ e);} static_assert (! is_same::value, "pointer type defines element_type or is like SomePointer");}

For this piece of code, it actually looks a little confused at first, but it remains the same. A class is defined and finally used by others, so for a class type, we just need to understand what its public members do. Then we probably know the role of this class.

Here you need to explain the role of _ _ detected_or_t, which is also a type template, declared as follows:

Template using _ _ detected_or_t = typename _ _ detected_or::type

The effect is that if _ Op is a valid type, then the type is _ Op, otherwise it is _ Default.

So for the class template pointer_traits, its public member function is as follows:

Pointer, which is actually an alias for the template parameter _ ptr

Element_type, is also an alias. If the type _ ptr::element_type exists, it is the type _ ptr::element_type. If the type _ ptr::element_type does not exist, but _ ptr is a template specialization, then it is _ ptr, otherwise it is _ _ undefined, which is actually a meaningless type.

Difference_type, which is also an alias, is _ ptr::difference_type if the type _ ptr::difference_type exists, otherwise it is the type ptrdiff_t.

Templateusing rebind, which is a type alias template, which is determined by the template parameters of class pointer_traits and the template parameters of rebind. If the type _ ptr::rebind exists, it is _ ptr::rebind. Otherwise, according to the implementation of the type template _ _ replace_first_arg, if _ ptr is template specialization _ Template, it is _ type, otherwise there is no type.

Pointer_to, which is a static member function, calls the pointer_to function of the template type, so the exact function depends on the implementation of _ ptr, but literally it should be to get the address of the element_type type object.

So generally speaking, the class template pointer_traits is actually used to obtain some type attributes of the template parameter _ ptr, so if you deduce from here, you can also know what attributes this template parameter type needs to have.

2.2 specialized pointer_traits

Next, take a look at the source code implementation of the specialized class template pointer_traits:

Template struct pointer_traits {typedef _ Tp* pointer; / / typedef _ Tp element_type; / / alias the template type typedef ptrdiff_t difference_type; template using rebind = _ Up*; static pointer pointer_to (_ _ make_not_void& _ r) noexcept {return std::addressof (_ r);}}

For specialized types, its public members are actually consistent with non-specialization, except that it is a specialization for the _ Tp* type. For other public members, it is relatively simple, so I won't say any more. Let's focus on the type alias template template using rebind, which directly gets a pointer of the _ Up* type. As a whole, its function is to rebind type member template aliases. So that the pointer type pointing to _ Tp can be obtained from the pointer type pointing to _ Up.

After the source code analysis, I seem to be a little impressed, but how exactly should we use it?

3. Simple use of pointer extractor

Let's write an example code first, as follows:

# include # include / / translate gcc compiled types into real types const char* GetRealType (const char* p_szSingleType) {const char* szRealType = abi::__cxa_demangle (p_szSingleType, nullptr, nullptr, nullptr); return szRealType;} int main () {using ptr = typename std::pointer_traits::template rebind; ptr p1; const std::type_info & info = typeid (p1); std::cout

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report