Understand the basic concepts of C ++ non-type template parameters

Source: Internet
Author: User

The emergence of the C ++ programming language immediately attracted the attention of developers. It has the functions used in the C language and supports many programming styles. Today, we will introduce some basic concepts of C ++ non-type template parameters, so that you can have a deeper understanding of this content.

In my opinion, the C ++ non-type class template parameter is equivalent to a global constant role. The following example is used to describe non-type class templates. By redefining a Stack template, this chapter requires a fixed-size array as the element container, and the size of the array can be defined by the template user. Therefore, the template designer should provide an interface for users to define the size of the array. This requires non-type class template parameters. The following code can be used to explain this problem:

 
 
  1. # Include <iostream>
  2. # Include <string>
  3. # Include <cstdlib>
  4. # Include <stdexcept>
  5. Template <typename T, int MAXSIZE>
  6. Class Stack {
  7. Private:
  8. T elems [MAXSIZE];
  9. Int numElems;
  10. Public:
  11. Stack ();
  12. Void push (T const &);
  13. Void pop ();
  14. T top () const;
  15. Bool isEmpty () const {
  16. Return numElems = 0;
  17. }
  18. Bool isFull () const {
  19. Return numElems = MAXSIZE;
  20. }
  21. };
  22. Template <typename T, int MAXSIZE>
  23. Stack <T, MAXSIZE >:: Stack (): numElems (0)
  24. {
  25. // Do not do anything, just to initialize numElems.
  26. }
  27. Template <typename T, int MAXSIZE>
  28. Void Stack <T, MAXSIZE >:: push (T const & elem)
  29. {
  30. If (numElems = MAXSIZE)
  31. {
  32. Throw std: out_of_range ("Stack <>:: push () ==> stack is full .");
  33. }
  34. Elems [numElems] = elem;
  35. ++ NumElems;
  36. }
  37. Template <typename T, int MAXSIZE>
  38. Void Stack <T, MAXSIZE >:: pop ()
  39. {
  40. 47 if (numElems <= 0)
  41. {
  42. Throw std: out_of_range ("Stack <>:: pop: empty stack ");
  43. }
  44. -- NumElems;
  45. }
  46. Template <typename T, int MAXSIZE>
  47. T Stack <T, MAXSIZE >:: top () const
  48. {
  49. If (numElems)
  50. {
  51. Throw std: out_of_range ("Stack <>:: pop: empty stack ");
  52. }
  53. // Return the last element.
  54. Return elems [numElems-1];
  55. }
  56. Int main ()
  57. {
  58. Try
  59. {
  60. Stack <int, 20> int20Stack;
  61. Stack <int, 40> int40Stack;
  62. Stack <std: string, 40> stringStack;
  63. Int20Stack. push (7 );
  64. Std: cout <"int20Stack. top ():" <int20Stack. top () <std: endl;
  65. Int20Stack. pop ();
  66. StringStack. push ("HelloWorld! ");
  67. Std: cout <"stringStack. top ():" <stringStack. top () <std: endl;
  68. StringStack. pop ();
  69. StringStack. pop ();
  70. }
  71. Catch (std: exception const & ex)
  72. {
  73. Std: cerr <"Exception:" <ex. what () <std: endl;
  74. Return EXIT_FAILURE;
  75. }
  76. Return 0;
  77. }

The code above reveals the definition and usage of C ++ non-type class template parameters.

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.