In addition to Weibo, there is also WeChat
Please pay attention

WeChat public account
Shulou
2025-11-03 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article shows you how to use the C++ template, the content is concise and easy to understand, it will definitely brighten your eyes. I hope you can get something through the detailed introduction of this article.
The application of templates in C++ programming language greatly improves the efficiency of program development to a certain extent. Let's explain the basic concepts of C++ template in detail.
Some time ago, I re-learned Clayboy, mainly looking at C++ programming ideas and C++ design new thinking. We have a better understanding of the use of templates, which are summarized as follows:
Common examples of C++ templates are listed below:
1. Static members of C++ template class
Template
< typename T>Struct testClass {static int _ data;}; template
< >Int testClass
< char>:: _ data = 1; template
< >Int testClass
< long>:: _ data = 2; int main (void) {cout
< < boolalpha < < (1==testClass< char>:: _ data)
< < endl; cout < < boolalpha < < (2==testClass< long>:: _ data)
< < endl; } 2. C++模板类偏特化 template < class I, class O>Struct testClass {testClass () {cout
< < "I, O" < < endl; } }; template < class T>Struct testClass
< T*, T*>{testClass () {cout
< < "T*, T*" < < endl; } }; template < class T>Struct testClass
< const T*, T*>{testClass () {cout
< < "const T*, T*" < < endl; } }; int main( void ) { testClass< int, char>Obj1; testClass
< int*, int*>Obj2; testClass
< const int*, int*>Obj3;}
3. Class template + function template
Template
< class T>Struct testClass
{
Void swap (testClass
< T>&) {cout
< < "swap()" < < endl; } }; template < class T>Inline void swap (testClass
< T>& x
TestClass
< T>& y)
{
X.swap (y)
}
Int main (void)
{
TestClass
< int>Obj1
TestClass
< int>Obj2
Swap (obj1, obj2)
}
4. Class member function template
Struct testClass {template
< class T>Void mfun (const T & t) {cout
< < t < < endl; } template < class T>Operator T () {return T ();}}; int main (void) {testClass obj; obj.mfun (1); int I = obj; cout
< < i < < endl; } 5. 缺省C++模板参数推导 template < class T>Struct test {T a;}; template
< class I, class O=test< I>> struct testClass {I b; O c;}; void main () {}
6. Untyped C++ template parameters
Template
< class T, int n>Struct testClass {T _ t; testClass (): _ t (n) {}}; int main (void) {testClass
< int,1>Obj1; testClass
< int,2>Obj2;}
7. Empty template parameter
Template
< class T>Struct testClass
Template
< class T>Bool operator== (const testClass
< T>&
Const testClass
< T>&)
{
Return false
}
Template
< class T>Struct testClass
{
Friend bool operator==
< >(const testClass&, const testClass&)
}
Void main ()
{
}
8. Template template class
Struct Widget1 {template
< typename T>T foo () {}}; template
< template< class T>Class X > struct Widget2 {}; void main () {cout < < 3 < <'\ nails;} the above is how to use C++ templates. Have you learned any knowledge or skills? If you want to learn more skills or enrich your knowledge reserve, you are welcome to follow the industry information channel.
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.