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