C + + Template programming-fifth Chapter technical Basics

Source: Internet
Author: User

Keyword Typename
1 template<typename t>2class  someclass3{4     TypeName T::subtype * ptr; 5 };

If no typename,t::subtype is considered to be a static member.

A Practical Example:

1 //print elements in a STL container2Template<typename t>3 voidPrint (TConst&con)4 {5 TypeName T::const_iterator Pos;6 TypeName T::const_iterator End (Con.end ());7     8      for(Pos=con.begin (); pos!=end; pos++)9         ....Ten}

If you are in a template and want to invoke another template function, you may need to use the. Template

1 template<int n>2voidconst & BS)3{  4     std::cout<<bs.template to_string<char, char_traits<Char allocator<char> > ();  // Here we are inside a template and calling a template function 5 }

In verse 5.2, the book's statement and my running results diverge, my operating environment is vs2013.

1#include <iostream>2 3 using namespacestd;4 5 voidfoo ()6 {7cout <<"Global Foo"<<Endl;8 }9 TenTemplate<typename t> One classbase{ A  Public: -     voidfoo () -     { thecout <<"Foo in Base"<<Endl; -     } - }; -  +Template<typename t> - classDerived1:base < T > + { A  Public: at     voidgoo () -     { -Foo ();//Foo in Base -Base<t>::foo ();//Foo in Base -:: foo ();//Global Foo -     } in }; -  to intMain () + { -derived1<int>D1; the D1.goo (); *     return 0; $}

The book says that the first Foo call in Goo is not actually called Foo in base, but my running results negate the book's writing. So it might be a matter of compiling the environment. To avoid uncertainty, add base<t>:: prefix, or use this pointer to compare insurance, to avoid uncertainty.

Changlog:

2015/6/18 to scroll to see the mathematical equation, first write so much.

C + + Template programming-fifth Chapter technical Basics

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.