Common usage of C ++ templates

Source: Internet
Author: User

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

  1. Template<TypenameT>Struct testclass
  2. {
  3. Static int _ data;
  4. };
  5. Template<>Int testclass<Char>: _ DATA=1;
  6. Template<>Int testclass<Long>: _ DATA=2;
  7. Int main (void ){
  8. Cout< Boolalpha <(1= Testclass<Char>: _ Data)< Endl;
  9. Cout< Boolalpha <(2= Testclass<Long>: _ Data)< Endl;
  10. }

2. The C ++ template class is special.

  1. Template<ClassI, Class O>Struct testclass
  2. {
  3. Testclass () {cout<"I, O"< Endl;}
  4. };
  5. Template<ClassT>Struct testclass<T*, T *>
  6. {
  7. Testclass () {cout<"T *, T *"< Endl;}
  8. };
  9. Template<ClassT>Struct testclass<ConstT *, T *>
  10. {
  11. Testclass () {cout<"Const T *, T *"< Endl;}
  12. };
  13. Int main (void)
  14. {
  15. Testclass<Int, Char>Obj1;
  16. Testclass<Int*, Int *>Obj2;
  17. Testclass<ConstInt *, int *>Obj3;
  18. }

3. Class templates + function templates

  1. Template<ClassT>Struct testclass
  2. {
  3. Void swap (testclass<T>&) {Cout<"Swap ()"< Endl;}
  4. };
  5. Template<ClassT>Inline void swap (testclass<T>& X,
    Testclass<T>& Y)
  6. {
  7. X. Swap (y );
  8. }
  9. Int main (void)
  10. {
  11. Testclass<Int>Obj1;
  12. Testclass<Int>Obj2;
  13. Swap (obj1, obj2 );
  14. }

4. class member function Template

  1. Struct testclass
  2. {
  3. Template< ClassT>Void mfun (const T & T)
  4. {
  5. Cout< <T < <Endl;
  6. }
  7. Template< ClassT>Operator T ()
  8. {
  9. Return T ();
  10. }
  11. };
  12. Int main (void)
  13. {
  14. Testclass OBJ;
  15. OBJ. mfun (1 );
  16. IntI=OBJ;
  17. Cout< <I < <Endl;
  18. }

5. Derivation of default C ++ template parameters

  1. Template< ClassT>Struct Test
  2. {
  3. T;
  4. };
  5. Template< ClassI, ClassO=Test< I> >Struct testclass
  6. {
  7. I B;
  8. O C;
  9. };
  10. Void main ()
  11. {
  12. }

6. Non-type C ++ template parameters

  1. Template< ClassT, int n>Struct testclass {
  2. T _ t;
  3. Testclass (): _ T (n ){
  4. }
  5. };
  6. Int main (void ){
  7. Testclass< Int, 1>Obj1;
  8. Testclass< Int, 2>Obj2;
  9. }

7. Empty template parameters

  1. Template< ClassT>Struct testclass;
  2. Template< ClassT>BoolOperator= (Const testclass< T>&,
    Const testclass< T>&)
  3. {
  4. Return false;
  5. };
  6. Template< ClassT>Struct testclass
  7. {
  8. Friend boolOperator=< >
    (Const testclass &, const testclass &);
  9. };
  10. Void main ()
  11. {
  12. }

8. template class

  1. Struct widget1
  2. {
  3. Template< TypenameT>
  4. T Foo (){}
  5. };
  6. Template< Template< ClassT>Class X>
  7. Struct widget2
  8. {
  9. };
  10. Void main ()
  11. {
  12. Cout< <3 < <'\ N ';
  13. }

The above is an introduction to some common methods of the C ++ template.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.