🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

2 total messages Started by rjl Thu, 14 Dec 1995 20:59
Re: alias name of parameterized class
#2936
Author: rjl
Date: Thu, 14 Dec 1995 20:59
29 lines
1716 bytes
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.

Regards,
	Rohan


-- 
----------------------------------------------------------------------------
rjl@iassf.easams.com.au	| All quotes can be attributed to my automated quote
Rohan Lenard            | writing tool.  Yours for just $19.95; and if you
+61-2-367-4555          | call now you'll get a free set of steak knives ...

[ 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. ]
Re: alias name of parameterized class
#2953
Author: John Max Skaller
Date: Tue, 19 Dec 1995 17:20
101 lines
3651 bytes
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. ]
Thread Navigation

This is a paginated view of messages in the thread with full content displayed inline.

Messages are displayed in chronological order, with the original post highlighted in green.

Use pagination controls to navigate through all messages in large threads.

Back to All Threads