Topic: Partial specializations? (Was: Sealed Classes)


Author: clamage@Eng.Sun.COM (Steve Clamage)
Date: 14 Mar 1995 16:59:37 GMT
Raw View
osinski@merv.cs.nyu.edu (Ed Osinski) writes:

>In article <D552nG.6qw@ucc.su.OZ.AU>, maxtal@Physics.usyd.edu.au (John Max Skaller) writes:
>|>  Fourthly, the committee is probably considering
>|> major extensions to the language right now in Austin.

No major extensions were adopted, and some were rejected.

>|> (I refer to partial specialisations).

Partial speciali[sz]ations of nested templates were rejected as being
unimplementable. Partial specializations of non-nested templates
are still allowed.


>This has certainly whetted my appetite.  John, could you, or someone else in
>the know, let the rest of us know more or less what "partial specializations"
>are?

If a template has more than one parameter, it might be appropriate
to provide a specialization based on only some of the parameters.
Simple example, although not too realistic:

// generic version, ignoring const, T must have operator[]()
template<class T, int size> int compare(T t1, T t2)
{
    for( int i=0; i < size && t1[i]==t2[i]; ++i )
 ;
    if( i == size ) return 0;
    if( t1[i] < t2[i] return -1;
    return 1;
}

// specialize for char*, ignoring const, and "size" doesn't matter
int compare<char*, int size>(char* t1, char* t2)
{
    return strncmp(t1, t2, size); // more efficient than above code
}
--
Steve Clamage, stephen.clamage@eng.sun.com




Author: maxtal@Physics.usyd.edu.au (John Max Skaller)
Date: Wed, 15 Mar 1995 21:33:35 GMT
Raw View
In article <3k2sri$8d@thecourier.cims.nyu.edu>,
Ed Osinski <osinski@cs.nyu.edu> wrote:
>In article <D552nG.6qw@ucc.su.OZ.AU>, maxtal@Physics.usyd.edu.au (John Max Skaller) writes:
>|>  Fourthly, the committee is probably considering
>|> major extensions to the language right now in Austin.
>|> (I refer to partial specialisations). At least, I sure
>|> hope they are, because I think they are important,
>|> and so does the Australian National body. And I suspect
>|> several other National Bodies will agree, as well as
>|> the individual members of the committee.
>
>This has certainly whetted my appetite.  John, could you, or someone else in
>the know, let the rest of us know more or less what "partial specializations"
>are?

 Sure. Even better, I've been told that they _have_ been accepted
at least in some form. First, consider a class template:

 template<class T, class U> class X { .. };

As you know you can write a specialisation:

 class X<int, int> { ... };

But what if you only want to specialise the first argument?
Can you write:

 template<class U> class X<int, U> { .. };

?? Well, the answer is: two weeks ago you could not, but
I believe now you can. Here's another example:

 template<class T> class X<T*, T***> { ... }

The resolution of which definition to use is done by
asking which of the "generic bindings" which might
apply to a particular instance is most specific (i.e. most
specialised). For example:

 <T*, T***> is more special than <A, B**>

Thats classes. Now consider the problem:

 template<class T> void f(T);
 template<class P> void f(P*);
 int i;
 f(&i); // Ambiguous??

Both templates can generate a definition of

 f(int*)

Which one should be used? Two weeks ago, this was ambiguous,
now the answer is "the second template, because it is
more specialised".

You will note "specialisations" form a DAG. Do not be
surprised at the analogy with (multiple) inheritance: virtual
function overriding is exactly the same kind of specialisation
as template specialisations. (The principal difference is
the binding time)
-----------------------------------------------------------------

I'm not aware of the exact rules. I'm sure there will be some
interesting problems, however. But one thing is for sure:
templates without partial specialisations would fail completely
to be orthogonal enough to construct significant libraries.

In particular, partial specialisations are necessary to make STL
maximally useful.

--
        JOHN (MAX) SKALLER,         INTERNET:maxtal@suphys.physics.su.oz.au
 Maxtal Pty Ltd,
        81A Glebe Point Rd, GLEBE   Mem: SA IT/9/22,SC22/WG21
        NSW 2037, AUSTRALIA     Phone: 61-2-566-2189




Author: osinski@merv.cs.nyu.edu (Ed Osinski)
Date: 14 Mar 1995 01:51:46 GMT
Raw View
In article <D552nG.6qw@ucc.su.OZ.AU>, maxtal@Physics.usyd.edu.au (John Max Skaller) writes:
|>  Fourthly, the committee is probably considering
|> major extensions to the language right now in Austin.
|> (I refer to partial specialisations). At least, I sure
|> hope they are, because I think they are important,
|> and so does the Australian National body. And I suspect
|> several other National Bodies will agree, as well as
|> the individual members of the committee.

This has certainly whetted my appetite.  John, could you, or someone else in
the know, let the rest of us know more or less what "partial specializations"
are?

|> --
|>         JOHN (MAX) SKALLER,         INTERNET:maxtal@suphys.physics.su.oz.au
|>  Maxtal Pty Ltd,
|>         81A Glebe Point Rd, GLEBE   Mem: SA IT/9/22,SC22/WG21
|>         NSW 2037, AUSTRALIA     Phone: 61-2-566-2189

--
---------------------------------------------------------------------
 Ed Osinski
 Computer Science Department, New York University
 E-mail:  osinski@cs.nyu.edu
---------------------------------------------------------------------
In the early years of the 16th century, to combat the rising tide of
religious unorthodoxy, the Pope gave Cardinal Ximinez of Spain leave
to move without let or hindrance throughout the land, in a reign of
violence, terror and torture that makes a smashing film.  This was
the Spanish Inquisition...
    -- Monty Python's Flying Circus