Interpretation of three special _c languages of class templates in C + + programming

Source: Internet
Author: User

1. Class template explicit-specific
in order to be specific, you first need a generic version, called the main template. The main template uses the standard library heap algorithm. A heap is a linear, tree-shaped structure that presses a value into a heap, is actually equal to inserting the value into a tree structure; Removing a value from the heap is equal to removing and returning the maximum value in the heap. But when you handle a pointer to a character, you touch the nail. The heap is organized according to the value of the pointer. We can provide an explicit, specific version to resolve this problem (example 1) If you want to provide a heap for char *, in addition to a heap for const char* (example 2)

Main template
Template <typename t>
class Heap
{
private:
  std::vector<t> H_;
Public:
  void push (const t& val);
  T pop ();
  the bool Empty () const//CONST declaration indicates at the end that the function cannot modify the class variable
  {return
    h_.empty ();
  }

Template <typename t>
void Heap<t>::p ush (const t& val)
{
  h_.push_back (val);
  std::p ush_heap (H_.begin (), H_.end ());

Template <typename t>
T head<t>::p op ()
{
  std::p op_head (H_.begin (), H_.end ());
  T-tmp (H_.back ());
  H_.pop_back ();
  return tmp;
}

Example 1

Display a special version
/***********************************************
 *     You can see that the template argument list is empty, but it's not a modular board at all  . Because no template parameters are specified. Therefore, the explicit specificity of the template is  called "complete specificity".
 *     Heap<const char*> is completely special, does not cause the template instantiation;
 *     heap<int> Special, will lead to the instantiation of the template;
 *     The compiler checks the class template for specificity based on the declaration of the main template.
/
template<>//Note that without any arguments, of course, it would not have been a template
class head <const Char *>
{
private:
  std::vector<const char *> h_;
Public:
  void push (const char *pval);
  const char * POP ();
  the bool Empty () const//CONST declaration indicates at the end that the function cannot modify the class variable
  {return
    h_.empty ();
  }
;
Again, Head<const Char *> is not a template
void Heap<const char*>::p ush (const char *pval)
{
  H_.push_ Back (pval);
  std::p ush_heap (H_.begin (), H_.end ());

Example 2

/***********************************************
 *     C + + does not require that an explicit-specific interface be fully matched to the interface of the main template  . In this case, No empty function is defined for the main template, and
 the  size and capitalize two functions are added on their own.
 *     Reminder: This example does not define the empty function is undesirable, the definition of the template's
 *  explicit specificity and the derivation of the class, although there is no technical connection, but
 *  is the user can still refer to the advantages of the derivation of the class, so that the special version has at least
 *  basic capabilities of the main template.
/
template<>//Note that without any arguments, of course, it would not have been a template
class head <char *>
{
private:
  std::vector<char *> h_;
Public:
  void push (char *pval);
  char * POP ();
  Note that the empty function is not provided here, YO!!!
  size_t size () const;
  void capitalize ();

2. Template local specificity
template local specificity The first thing to declare is that C + + does not support the local specificity of the function template, so here we only discuss the local specificity of the class template. We still need a main template first. (Reference class template explicit customization) self-understanding: If the target can not be defined by the different complete specificity, it is too troublesome, there is no better way? That's local specificity. (Example 1) Hint: Local specificity it is a template. Complete specificity is not the same as the template.

Example 1

/***********************************************
 *  local specificity 
 *     and complete specificity are different, here the heap parameter type is only partially true
 * is  defined as t*, and T is an unspecified type, which is why it is part  of the "special" reason;
 *  local specificity takes precedence over the main template when a heap is instantiated using an unmodified pointer type;
 *     when a const char * or char * (Reference class template is explicitly specific) is used
 to  instantiate the heap, then full specificity takes precedence over local specificity.
 *  heap<std::string> H1;  The main template  T is std::string
 *  heap<std::string *> h2;  Local specificity  T is std:string
 *  heap<int **> h3;    Local specificity T is an int *
 *  Heap<char *> h4;  Completely special T is char *
 *  heap<const int *> h5;  Local specificity T is a const int
 *  heap<int (*) () > h6;  Local special T is int ()
***********************************************/
template <typename t>
class Heap<t *>//Note here
{
private:
  std::vector<t *>h_;
Public:
  void push (const T *val);
  T *pop ();
  bool Empty ()
  {return
    h_.empty ();
  }
};

Template <typename t>
void heap<t *>::p ush (const T *val)
{
  //...
}

Example 2

/***********************************************
 * is     subtle but useful: the complete specificity or local specificity of the main template
 *  Must be instantiated with the same number and type of arguments as the main template, but the
 parameters of its  template do not need to have the same form as the main template.
///
define a template with three template parameters, written as follows
template <typename R, TypeName A1,typename a2> 
/Note that in local specificity, the template parameter is also three, but the writing form is not the same.
class Heap<r (*) (A1,A2) >
{
  // ......
};

Heap<char * (*) (int,int) > H7; R is the char *,a1 and A2 are int
////Think of char * (*) (Int,int) as a "pointer to a non-member function with two arguments"

template <class c,typename t>
class heap<t C::* >
{
  //......
};
Heap<std::string Name::* > h8;//t is string,c is name

Although it is only a guess why we need to use heap for these things, I know there is such a usage!

3. class Template Membership Special

Although there is no relationship between the specificity of the template and the derivation of the class, it may be useful to draw on the derivation in the special template. It also means that a complete or localized specificity usually has to be implemented with all the capabilities of the main template.
Cases:

Main template
Template <typename t>
class Heap
{
private:
  std::vector<t> H_;
Public:
  void push (const t& val);
  T pop ();
  the bool Empty () const//CONST declaration indicates at the end that the function cannot modify the class variable
  {return
    h_.empty ();
  }
What we really need to be special is push and pop two functions.
Contrast explicit specificity, it is through the main template, and then write a template explicit version of the class;
And this is just a separate special for class template members.
template<>
void Heap<const char*>::p ush (const char *const &pval)
{
  H_.push_back (pval );
  std::p ush_heap (H_.begin (), H_.end (), strless);

template<>
const char* heap<const char*>::p op ()
{std:pop_heap ()
  , H_.begin (), strless);
  CONST char* TMP = H_.back ();
  H_.pop_back ();
  return tmp;
}


Note that the interfaces of these functions must match the corresponding interfaces of the template that they are making special members. In Example 1, it is necessary to match the interface of the main template. And if you are defining an explicit/localized version of the class yourself, you do not need to match the same. (see explicit specificity and local specificity), and finally point out two points: first, in addition to member functions, in fact, members can also be explicitly customized, such as static members and member templates. Second, explicit specificity is a means of providing a custom version of a template or template member, whereas explicit instantiation simply tells the compiler to instantiate a member.

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.