Topic: alias name of parameterized class


Author: TABATA Tomohira <loba@k2.t.u-tokyo.ac.jp>
Date: 1995/12/13
Raw View
Is the following use of typedef allowed to make alias names of
a parameterized class?

template <class T> ParameterizedClass { /* ... */ };
typedef ParameterizedClass PC;

/* ... */
{
 PC<int> foo;
}

Or, this may be better than the second line,

template <class T> typedef ParameterizedClass PC<T>;

------
   Tomohira Tabata loba@k2.t.u-tokyo.ac.jp
   Department of Mathematical Engineering and Information Physics,
   Faculty of Engineering, University of Tokyo, Japan
   TEL: +81-3-3812-2111 ext. 6902    FAX: +81-3-5800-6969


---
[ comp.std.c++ is moderated.  Submission address: std-c++@ncar.ucar.edu.
  Contact address: std-c++-request@ncar.ucar.edu.  The moderation policy
  is summarized in http://dogbert.lbl.gov/~matt/std-c++/policy.html. ]





Author: p150650@cs.tut.fi (Tero Pulkkinen)
Date: 1995/12/14
Raw View
In article <199512140426.NAA01763@k9.k2.t.u-tokyo.ac.jp> loba@k2.t.u-tokyo.ac.jp (Tomohira Tabata) writes:

   I wonder we have no method of giving a template a different name.
   Typedef provides a new concept to a type, for example.
    typedef int size_t;
   In the same manner I'd like to give a alias to a template. I know it
   is possible using #define.
    #define MyContainer Vector
   We need a method independent of the preprocessor for the purpose.

Doesnt this work?

template <class T> typedef Vector<T> MyContainer;

Well, it didnt work with g++2.7.0. Though i dont know why wouldn't it work.
Reading the september working paper i didnt found anything, what would
forbid using it, and typedef is a simple-declaration and should be allowed
in template declaration, wouldnt it? [temp.2], [dcl.dcl.1], [dcl.spec.1]
So i guess its not yet implemented in g++ or i cant read the draft...

--
-- Tero Pulkkinen -- terop@kotka.cs.tut.fi --



---
[ comp.std.c++ is moderated.  Submission address: std-c++@ncar.ucar.edu.
  Contact address: std-c++-request@ncar.ucar.edu.  The moderation policy
  is summarized in http://dogbert.lbl.gov/~matt/std-c++/policy.html. ]





Author: John Max Skaller <maxtal@suphys.physics.su.oz.au>
Date: 1995/12/19
Raw View
rjl@iassf.easams.com.au wrote:
>In article <199512140426.NAA01763@k9.k2.t.u-tokyo.ac.jp> you write:
>: I wonder we have no method of giving a template a different name.
>: Typedef provides a new concept to a type, for example.
>:  typedef int size_t;
>: In the same manner I'd like to give a alias to a template. I know it
>: is possible using #define.
>:  #define MyContainer Vector
>: We need a method independent of the preprocessor for the purpose.
>
>How about this -
>
>template <class X> class Y : public Vector<X> {};
>
>A template Y<T> is effectively a synonym for a Vector<X>.   A good
>compile should have no difficulty with this.

  This doesn't work well at all. For a start, you have to
"redeclare" all the constructors. But more important,
it refuses to copy a Vector<X> to a Y<X> (that would be a downcast).
In particular you cannot insert a Vector<X> into a list<Y<X> >.

  What you CAN do is:

  template<class T> struct SILLY {
    typedef Vector<T> Y;
  };

and now SILLY<X>::Y is a genuine synonym for Vector<X>.

-------------------------------------------------------------------

  What we ACTUALLY NEED is NOT an aliasing facility, but a way
to define a NEW entity. In particular consider:

  template<
    class T,
    template<class T> class Container
  >
  class Wrapper {
     Container<T> container;
  };


This template requires a CONTAINER TEMPLATE as an argument.
AND it must have one parameter. Now, ignoring allocators,
how would you wrap a set?

You can't without defining a new Wrapper2 template,
since set has two parameters. But one would like:


  template<class T> class set_less = set<T, less<T> >;

which is a new PRIMARY template that can be used like:

  Wrapper<set_less, int> x;

The template body is generated in the obvious way from the
template (and specialisations) of set, but it can be
specialised, and it can be specialised independently of set.

BTW: I'm told the general name of this technique is called
"Currying", it is a special case of the
mathematical notion of a projection, which is a special
case of the notion of specialisation, which is the
addition of ANY constraint.

Another use:

  template<class T> class selfmap = map<T,T>;

reduces map from 2 arguments to 1.

Oh, you may have noticed this kind of idea is WEAKLY supported
by the notion of default arguments. OTOH for functions,
existing support is strong __and complete__: rewrapping
functions, unlike classes, has no bad side effects,
it is efficient, and it requires a fixed small amount
of verbage:

  void ff(int a) { f(a,a); }

for example is reasonably effective and the idea extends directly
to function templates.


--
John Max Skaller               voice: 61-2-566-2189
81 Glebe Point Rd              fax:   61-2-660-0850
GLEBE NSW 2037                 email: maxtal@suphys.physics.oz.au
AUSTRALIA                      email: skaller@maxtal.com.au




---
[ comp.std.c++ is moderated.  Submission address: std-c++@ncar.ucar.edu.
  Contact address: std-c++-request@ncar.ucar.edu.  The moderation policy
  is summarized in http://dogbert.lbl.gov/~matt/std-c++/policy.html. ]