13.6 Typedef Templates

Ru-Brd

Class templates are often combined in relatively sophisticated ways to obtain other parameterized types. When such parameterized types appear repeatedly in source code, it is natural to want a shortcut for them, just as typedefs provide a shortcut for unparameterized types.

Therefore, C++ language designers are considering a construct that may look as follows :

 template <typename T>  typedef vector<list<T> > Table; 

After this declaration, Table would be a new template that can be instantiated to become a concrete type definition. Such a template is called a typedef template (as opposed to a class template or a function template). For example:

 Table<int> t;  //  t  has type  vector<list<int> > 

Currently, the lack of typedef templates is worked around by using member typedefs of class templates. For our example we might use:

 template <typename T>  class Table {    public:      typedef vector<list<T> > Type;  };  Table<int>::Type t;  //  t  has type  vector<list<int> > 

Because typedef templates are to be full-fledged templates, they could be specialized much like class templates:

  // primary typedef template:  template<typename T> typedef T Opaque;  // partial specialization:  template<typename T> typedef void* Opaque<T*>;  // full specialization:  template<> typedef bool Opaque<void>; 

Typedef templates are not entirely straightforward. For example, it is not clear how they would participate in the deduction process:

 void candidate(long);  template<typename T> typedef T DT;  template<typename T> void candidate(DT<T>);  int main()  {      candidate(42);  // which  candidate()  should be called?  } 

It is not clear that deduction should succeed in this case. Certainly, deduction is not possible with arbitrary typedef patterns.

Ru-Brd


C++ Templates
C++ Templates: The Complete Guide
ISBN: 0201734842
EAN: 2147483647
Year: 2002
Pages: 185

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net