The template application in C ++ programming language has greatly improved the efficiency of program development to a certain extent. In this article, we will explain in detail the basic concepts of C ++ templates. I hope that beginners can fully master this knowledge through the content introduced in this article.
Some time ago, I re-learned C ++, mainly focusing on C ++ programming ideas and C ++ new design ideas. I have a better understanding of the use of the template, which is summarized as follows:
Common C ++ templates are listed below:
1. Static members of the 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. The C ++ template class is special.
- 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 templates + function templates
- 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. Derivation of default C ++ template parameters
- template < class T> struct test
- {
- T a;
- };
- template < class I, class O=test< I> > struct testClass
- {
- I b;
- O c;
- };
- void main()
- {
- }
6. Non-type 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 parameters
- 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 class
- struct Widget1
- {
- template< typename T>
- T foo(){}
- };
- template< template< class T>class X>
- struct Widget2
- {
- };
- void main()
- {
- cout< < 3 < < '\n';
- }
The above is an introduction to some common methods of the C ++ template.