Http://developer.51cto.com/art/201002/182202.htm
C ++Programming LanguageTo a certain extent, the template application inProgramDevelopment efficiency. Here we areArticleI want to 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<TypenameT>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<ClassI, Class O>Struct testclass
-
- {
-
- Testclass () {cout<"I, O"< Endl;}
-
- };
-
- Template<ClassT>Struct testclass<T*, T *>
-
- {
- Testclass () {cout<"T *, T *"< Endl;}
-
- };
-
- Template<ClassT>Struct testclass<ConstT *, T *>
-
- {
-
- Testclass () {cout<"Const T *, T *"< Endl;}
-
- };
- Int main (void)
-
- {
-
- Testclass<Int, Char>Obj1;
-
- Testclass<Int*, Int *>Obj2;
-
- Testclass<ConstInt *, int *>Obj3;
-
- }
3. Class templates + function templates
-
- Template<ClassT>Struct testclass
- {
-
- Void swap (testclass<T>&) {Cout<"Swap ()"< Endl;}
-
- };
-
- Template<ClassT>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< ClassT>Void mfun (const T & T)
-
- {
-
- Cout< <T < <Endl;
-
- }
-
- Template< ClassT>Operator T ()
- {
-
- Return T ();
-
- }
-
- };
-
- Int main (void)
-
- {
-
- Testclass OBJ;
-
- OBJ. mfun (1 );
-
- IntI=OBJ;
- Cout< <I < <Endl;
-
- }
5. Derivation of default C ++ template parameters
-
- Template< ClassT>Struct Test
-
- {
-
- T;
-
- };
- Template< ClassI, ClassO=Test< I> >Struct testclass
-
- {
-
- I B;
-
- O C;
-
- };
-
- Void main ()
-
- {
-
- }
6. Non-type C ++ template parameters
- Template< ClassT, 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< ClassT>Struct testclass;
- Template< ClassT>BoolOperator= (Const testclass< T>&,
Const testclass< T>&)
-
- {
-
- Return false;
-
- };
- Template< ClassT>Struct testclass
-
- {
-
- Friend boolOperator=< >
(Const testclass &, const testclass &);
-
- };
-
- Void main ()
-
- {
-
- }
8. template class
-
- Struct widget1
-
- {
- Template< TypenameT>
-
- T Foo (){}
-
- };
-
- Template< Template< ClassT>Class X>
-
- Struct widget2
-
- {
-
- };
-
- Void main ()
-
- {
- Cout< <3 < <'\ N ';
-
- }
The above is an introduction to some common methods of the C ++ template.