Topic: partial specialization


Author: 100754.2730@compuserve.com (Martin Aupperle)
Date: 1996/05/23
Raw View
I have heard something about "partial specialization", together with
an example like this (exact example has been lost):

  template<class T> f( T )  { puts( "#1" ); )
  template<class T> f( T* ) { puts( "#2" ); }

Now if we have

  int* ip;
  f( ip );

both flavours of f can theoretically be instantiated. It was said,
that #2 would be the correct one.

Why is this called "partial specialization" ? Why partial? And what is
specialized?

Why isn't  that simply ambiguous? The user might think the other one
gets instantiated. I remember the overlading/user defined conversion
thing, where ambiguous cases are reported as such.

Can someone of the gurus explain that?
Thanks - Martin





-----------------------------------
Signatures are a waste of bandwidth
-----------------------------------
---
[ 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         ]
[ FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html    ]
[ Policy:   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu                             ]





Author: vandevod@cs.rpi.edu (David Vandevoorde)
Date: 1996/05/24
Raw View
>>>>> "MA" == Martin Aupperle <100754.2730@compuserve.com> writes:

MA> I have heard something about "partial specialization", together with
MA> an example like this (exact example has been lost):

MA>   template<class T> f( T )  { puts( "#1" ); )
MA>   template<class T> f( T* ) { puts( "#2" ); }

MA> Now if we have

MA>   int* ip;
MA>   f( ip );

MA> both flavours of f can theoretically be instantiated. It was said,
MA> that #2 would be the correct one.

Correct.

MA> Why is this called "partial specialization" ? Why partial? And what is
MA> specialized?

Because this doesn't specialize f ``fully''; i.e., the result is still
parameterized (i.e. a template). ``full specialization'' would be
something like:
 template<> void f(void*) { puts("#0"); }

MA> Why isn't  that simply ambiguous? The user might think the other one

Because it's often useful to be able to do this ;-)
E.g., you might want a special treatment for a list of pointers
because a list<T*> could be implemented more efficiently than a
fully generic list<T>.

MA> gets instantiated. I remember the overlading/user defined conversion
MA> thing, where ambiguous cases are reported as such.

See next.

MA> Can someone of the gurus explain that?

To remove the ambiguity a (partial) ``order of preference''
had to be introduced. This partial order is in fact (IMO)
very intuitive. If you have two templates t<X1, ..., Xn>
and t<Y1, ..., Yn>, then the first is ``more specialized
that the second if you can derive it from the second by a
valid substitution of Y1, ..., Yn, but not vice versa.

Thus, when selecting which template to instantiate, determine
the ``most specialized'' ones and if this is unique, there is
no ambiguity.

 Daveed
---
[ 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         ]
[ FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html    ]
[ Policy:   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu                             ]





Author: 100754.2730@compuserve.com (Martin Aupperle)
Date: 1996/05/25
Raw View
vandevod@cs.rpi.edu (David Vandevoorde) wrote:

>To remove the ambiguity a (partial) ``order of preference''
>had to be introduced. This partial order is in fact (IMO)
>very intuitive. If you have two templates t<X1, ..., Xn>
>and t<Y1, ..., Yn>, then the first is ``more specialized
>that the second if you can derive it from the second by a
>valid substitution of Y1, ..., Yn, but not vice versa.

Thank you for this enlighting answer. The "order of preference" is
indeed very intuitive.
But what is a "valid substitution"? Is it the rule that X is a valid
substitution for Y if all occurences of Y can be replaced with X and a
syntactically correct program remains correct?

Martin

-----------------------------------
Signatures are a waste of bandwidth
-----------------------------------
---
[ 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         ]
[ FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html    ]
[ Policy:   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu                             ]