SGI STL Feature Extractor

Source: Internet
Author: User
Tags constructor

1 iterator-related types of extraction (iterator_traits)

Many algorithms, such as lookups, pointer movements, and so on, all pass through iterators, while the types of different iterators determine the difference in their algorithmic operating procedures.

Template <class iterator>
struct Iterator_traits {
  typedef typename Iterator::iterator_category Iterator_category;
  typedef typename Iterator::value_type        Value_type;
  typedef typename Iterator::d ifference_type   difference_type;
  typedef typename Iterator::p ointer           pointer;
  typedef typename Iterator::reference         reference;
};
For the native pointer type, the const pointer type of the iterator for the extract-biased version, note that the pointer-type iterator is a random-access iterator
Template <class t>
struct iterator_traits<t*> {
  typedef random_access_iterator_tag ITERATOR_ category;
  typedef T                          Value_type;
  typedef ptrdiff_t                  Difference_type;
  typedef t*                         Pointer;
  typedef t&                         Reference;
};

Template <class t>
struct iterator_traits<const t*> {
  typedef random_access_iterator_tag Iterator_category;
  typedef T                          Value_type;
  typedef ptrdiff_t                  Difference_type;
  typedef const T*                   pointer;
  typedef const t&                   reference;
};
Extraction function (Note that the template description is not required when the version function is called) type of the extraction iterator itself
Inline TypeName iterator_traits<iterator>::iterator_category
iterator_category (const iterator&) {
  typedef typename Iterator_traits<iterator>::iterator_category category;
  return category ();
}
Note that the extraction operation is a method of picking an int () to create a temporary object extraction iterator Spacing type: pointer type
Template <class iterator>
inline typename iterator_traits<iterator>::d ifference_type*
Distance _type (const iterator&) {
  return static_cast<typename iterator_traits<iterator>::d ifference_type* > (0);
}
extraction iterator refers to the type: identified by a pointer
Template <class iterator>
Inline typename iterator_traits<iterator>::value_type*
Value_type ( Const iterator&) {
  return static_cast<typename iterator_traits<iterator>::value_type*> (0);
}
2__type_traits data Type the extraction device is intended to extract whether the data types are pod data type, whether they have trivial constructors, copy functions, etc., assignment =, destructor
Basic type, guaranteed minimum level of setting
Template <class type> struct __type_traits {typedef __true_type This_dummy_member_must_be_first; /* Do not remove the this member.
                      It informs a compiler which automatically specializes __type_traits that this __type_traits template is special. It just makes sure that things work if an implementation is using a template C Alled __type_traits for something unrelated. */* The following restrictions should is observed for the sake of compilers which automatically produce type S  Pecific specializations of this class:-The reorder the members below if you wish-you may
            Remove any of the members below if you wish-you must don't rename members without making the corresponding Name change in the compiler-members you add would be a treated like regular members unless yo U Add the appropriate supporT in the compiler.
   */typedef __false_type Has_trivial_default_constructor;
   typedef __false_type Has_trivial_copy_constructor;
   typedef __false_type Has_trivial_assignment_operator;
   typedef __false_type Has_trivial_destructor;
typedef __false_type IS_POD_TYPE; };
Later, the partial version, including all the built-in types, with the native pointer type
Template <class t>
struct __type_traits<t*> {
   typedef __true_type    Has_trivial_default_ constructor;
   typedef __true_type    Has_trivial_copy_constructor;
   typedef __true_type    Has_trivial_assignment_operator;
   typedef __true_type    Has_trivial_destructor;
   typedef __true_type    is_pod_type;
};

Example: The scope of a given iterator, first extracting the iterator refers to the container element type, followed by the type to determine whether the class is necessary to call the constructor for the object's destruction process

Using Value_type to extract the container element type, note that the type is labeled with the pointer
template <class forwarditerator> the
inline void
__destroy_aux ( ForwardIterator first, ForwardIterator last, __false_type) {for
  (; first < last; ++first)
    Destroy (&*firs t);
}

Whether the __type_traits extraction type has the necessary destructor
template <class forwarditerator> the 
inline void __destroy_aux ( ForwardIterator, ForwardIterator, __true_type) {}

//have the necessary destructors for each element into the destructor
template <class ForwardIterator, Class t>
inline void __destroy (forwarditerator first, ForwardIterator last, t*) {
  typedef typename __TYPE_ Traits<t>::has_trivial_destructor Trivial_destructor;
  __destroy_aux (First, Last, Trivial_destructor ());
}

There is no need for destructors to process template
template <class forwarditerator>
inline void Destroy (ForwardIterator first, ForwardIterator last) {
  __destroy (first, Last, Value_type (first));
}

Ref:stl Source Code Analysis

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.