Get the App
SLTechnology News&Howtos  ›  Development  › 

What is the method of using C++ template?

Shulou Source: shulou.com Published: 2022-06-02 22:38:15 10月06日 Update

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.

Tags: Templates templates parameters templates usage methods content functions members skills knowledge programming conciseness brevity that is commonly used ideas situations efficiency articles Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Tech Info Shulou Information Microsoft Redmi Docker