Topic: templatedef


Author: "Michael Pryhodko" <mpryhodko@westpac.com.au>
Date: Wed, 12 Jan 2005 01:35:57 CST
Raw View
What do you think about this 'template typedef' proposal (I tried to
discuss it in comp.lang.c++.moderated):

----------------------< begin >-----------------------

// some templates
template<class T> class type {};
template<class T, class U> class ptr {};

// convenient shortcut
templatedef<class T, class U> ptr< T, type<U> > TmplAlias;

Rules are pretty the same as for 'typedef':
- just like typedef does not create new type 'templatedef' does not
create new template -- it is only convenient shortcut to given
'template-name + params' entity.
- it has similar syntax
- templatedef for template functions could be built to look similar to
typedef for usual functions.

I'd like to outline some advantages of this 'templatedef':
a) you can create fine-looking and human-readable complex template
specializations, e.g.:

template<class U> class TmplAlias<U, U> {};
is equivalent to:
template<class U> class ptr<U, type<U> > {};

b) you can change number of template parameters in order to pass
template as template template argument, e.g.:

template<class U, class T> class foo;
template<template<class A> class T> class B;

templatedef<class U> foo< U*, foo2<U> > TmplAlias;
B<TmplAlias> foo_var; // cannot be done without templatedef

c) consequently you can get around that nasty 'default template
parameters' problem, e.g.:

template<template<class A> class T> class B;
template<class T, class U = A> class Foo;

templatedef<class T> Foo<T> Foo_without_def_params;
// now Foo_without_def_params can be used whenever
// template with one parameter required, e.g.:

B<Foo_without_def_params> foo_var;

----------------------<  end  >-----------------------

I already read n1489 (and currently I am "reading through" the
collection of these notes and proposals). But here is my thoughts about
n1489:
1. I think that:
templatedef<class T> foo<T, U> t_alias;
looks more 'natural' than
template<class T> using t_alias = foo<T, U>;
because it more closely reflects 'typedef' syntax with respect to
templates (IMHO).

2. I disagree that (partial) template specialization using t-aliases
should be banned:
a) it looks natural to me
b) since t-alias is just substitution to more complex t-name it should
not create any problem in any existing template specialization logic
c) I was not convinced by the reasons described in n1489 :))

3. well... that paper introduces extensions to 'using' keyword more
than introducing 'template typedef' :)

Bye.
Sincerely yours, Michael.

P.S. Just want to influence C++ evolution :)

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]