Article View: comp.std.c++
Article #2953Re: alias name of parameterized class
From: John Max Skaller
Date: Tue, 19 Dec 1995 17:20
Date: Tue, 19 Dec 1995 17:20
101 lines
3651 bytes
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. ]
Technical Details
Message-ID:
<4b6m5p$5lr@oznet07.ozemail.com.au>Path:
arc1-ca.pugleaf.net!howland.reston.ans.net!agate!agate!mattReferences:
<199512141952.AA12197@hercules.iassf.easams.com.au>Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!howland.reston.ans.net!agate!agate!matt From: John Max Skaller <maxtal@suphys.physics.su.oz.au> Newsgroups: comp.std.c++ Subject: Re: alias name of parameterized class Date: 19 Dec 1995 17:20:30 GMT Organization: MAXTAL Approved: matt@Austern (Matt Austern) Message-ID: <4b6m5p$5lr@oznet07.ozemail.com.au> References: <199512141952.AA12197@hercules.iassf.easams.com.au> NNTP-Posting-Host: austern.hip.berkeley.edu