Retrieving settings templates, types and static methods of a class template

A bargain with classes templates, is making typedef (in public) to easily retrieve settings templates. Example: I have a class c1 <T>
and I want to recover the type T. This will be possible through and typedef typename.

Code:

template <typename T> typedef struct (my_class_t T data_t;);
int main () { typedef my_vector_t <int>: data_t data_t; }

However we can not implement the operator:: that if a member of the left is not an abstract data type (i.e. dependent on a type
template not yet rated). For example, if I want to manipulate the typedef "const_iterator" of class std:: vector provided by the STL,
if the parameters templates std:: vector are not affected the program will refuse to compile:

Code:
void write (const std:: vector <int> & v)
{ std:: vector <int>:: const_iterator lives (v.begin (), sells (v.end ());
for (; lives! = sells; ++ lives) std:: cout <<* lives " '';}
template <typename T>
void write (const std:: vector <int> & v)
{ std::vector <T>::const_iterator lives (v.begin(), sells(v.end()); // ERROR!
for (; lives! = sells; ++ lives) std:: cout <<* lives " '';}

Here the std:: vector <T> is located to the left of a: and depends on a parameter template. This is where typename comes in.

Code:

template <typename T>
void write (const std:: vector <int> & v)
{ typename std::vector <T>::const_iterator lives(v.begin(), sells(v.end());
for ( ; lives! = sells; ++ lives) std:: cout <<* lives " '';}

Note that when the type of a left: depends on a parameter template, it must be preceded by a typename. Given that the types quickly become heavy to handle, it is wise to make typedef. In another example, more complicated, it gives for example:
Code:
typedef typename std::vector <typename std::vector <T>::const_iterator>::const_iterator mon_type_t


Templates recursive

It is possible to define recursive templates (if so). An example:
Code:
# include <iostream>
template <int N>
int fact () { return N * fact <N-1> (); }
template <>
int fact <0> () {return 1;}
int main ()
{ std: : cout <<fact <5> () <<std:: endl; return 0; }

Here the interest is fairly moderate as we compile concrete fact <5>, fact <4> ... fact <0> is really just to give a simple example
template recursive.

What interest of a template recursive coup? Well a boost, the template allows recursive implement Tuples generic! For small curious that stands in / usr / include / boost / tuple / detail / tuple_basic.hpp, so I will not elaborate further ;-)


Tester values type template

It is possible boost to verify whether a type template is a type expected and block the compilation as appropriate. Given that it
uses the library boost, I just give a brief example:

<boost/type_traits/is_same.hpp> # include # include
<boost/static_assert.hpp> template <typename T> struct
my_struct_Test_int { BOOST_STATIC_ASSERT ((boost: is_same <T,int>: value)) / / .. . };