In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-09-20 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
C++ non-type template parameters of the basic concept, many novices are not very clear, in order to help you solve this problem, the following editor will explain in detail for you, people with this need can come to learn, I hope you can get something.
The C++ programming language has attracted the attention of developers immediately after its advent. It has the functions of the C language and can support many programming styles. What we are going to introduce to you today is some basic concepts of C++ non-type class template parameters, so that you can have a more in-depth understanding of this aspect.
I think the C++ untyped class template parameter is equivalent to the role of a global constant. The following examples are given to illustrate non-type class templates. By redefining a Stack template, this chapter requires that a fixed-size array be used as the container for the element, and that the size of the array can be defined by the user of the template. Then, for the template designer, an interface should be provided so that the user can define the size of the array. This requires the use of non-type class template parameters. The following code explains the problem very well:
# include
< iostream># include
< string># include
< cstdlib># include
< stdexcept>Template
< typename T, int MAXSIZE>Class Stack {private: t elems [MAXSIZE]; int numElems; public: Stack (); void push (T const&); void pop (); T top () const; bool isEmpty () const {return numElems = = 0;} bool isFull () const {return numElems = = MAXSIZE;}}; template
< typename T, int MAXSIZE>Stack
< T, MAXSIZE>:: Stack (): numElems (0) {/ / do nothing but initialize numElems. } template
< typename T, int MAXSIZE>Void Stack
< T, MAXSIZE>:: push (T const& elem) {if (numElems = = MAXSIZE) {throw std::out_of_range ("Stack
< >:: push () = > stack is full. ");} elems [numElems] = elem; + + numElems;} template
< typename T, int MAXSIZE>Void Stack
< T, MAXSIZE>:: pop () {47 if (numElems
< = 0) { throw std::out_of_range("Stack< >:: pop: empty stack ");}-- numElems;} template
< typename T, int MAXSIZE>T Stack
< T, MAXSIZE>:: top () const {if (numElems) {throw std::out_of_range ("Stack
< >:: pop: empty stack ");} / / returns an element. Return elems [numElems-1];} int main () {try {Stack
< int, 20>Int20Stack; Stack
< int, 40>Int40Stack; Stack
< std::string, 40>StringStack; int20Stack.push (7); std::cout < < "int20Stack.top ():" < < int20Stack.top () < < std::endl; int20Stack.pop (); stringStack.push ("HelloWorld!"); std::cout < < "stringStack.top ():" < < stringStack.top () < < std::endl; stringStack.pop (); stringStack.pop () } catch (std::exception const& ex) {std::cerr < < "Exception:" < < ex.what () < < std::endl; return EXIT_FAILURE;} return 0;}
The above code reveals the definition and use of C++ untyped class template parameters.
Is it helpful for you to read the above content? If you want to know more about the relevant knowledge or read more related articles, please follow the industry information channel, thank you for your support.
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.
The market share of Chrome browser on the desktop has exceeded 70%, and users are complaining about
The world's first 2nm mobile chip: Samsung Exynos 2600 is ready for mass production.According to a r
A US federal judge has ruled that Google can keep its Chrome browser, but it will be prohibited from
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
About us Contact us Product review car news thenatureplanet
More Form oMedia: AutoTimes. Bestcoffee. SL News. Jarebook. Coffee Hunters. Sundaily. Modezone. NNB. Coffee. Game News. FrontStreet. GGAMEN
© 2024 shulou.com SLNews company. All rights reserved.