Topic: [Q] about template specialization
Author: Ruslan Shevchenko <Ruslan@Shevchenko.Kiev.UA>
Date: 1998/04/26 Raw View
look at the next code:
template<class K, class E>
inline
const K& default_key(const E&);
template<class T>
inline
const T& default_key(const T& x)
{
return x;
}
than, in
template<class K, class E>
class X
{
........
virtual const K& get_key(const E& x)
{ return default_key<K,E>(x); }
};
than, lets look on class X<Y,Y>
I think that X<Y,Y>::get_key must call specialization (i. e. second
definition)
Actually, in egcs it is wrong.
So, what standart behavior ?
Thanks.
--
@=
//RSSH mailto:Ruslan@Shevchenko.Kiev.UA
[ 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 ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: Oleg Zabluda <zabluda@math.psu.edu>
Date: 1998/04/28 Raw View
Ruslan Shevchenko <Ruslan@Shevchenko.Kiev.UA> wrote:
: look at the next code:
: template<class K, class E>
: inline
: const K& default_key(const E&);
: template<class T>
: inline
: const T& default_key(const T& x)
: {
: return x;
: }
: than, in
: template<class K, class E>
: class X
: {
: ........
: virtual const K& get_key(const E& x)
: { return default_key<K,E>(x); }
: };
: than, lets look on class X<Y,Y>
: I think that X<Y,Y>::get_key must call specialization (i. e. second
: definition)
: Actually, in egcs it is wrong.
: So, what standart behavior ?
First important thing is that there is no partial specialization
for function. Only for classes. All template functions and plain
functions exist in one big soup. Then the compiler chooses the right
one among them during overload resolution stage. Among other
things it figures out if some of your template functions is
``more specialized'' then others. ``More specialized'' has nothing
to do with partial specialization, and typical ``class''
logic does not apply.
Now, I have a great difficulty understanding the formal rules
from CD2. My reading is that neither of your get_key<>()'s
is more specialized then the other, and the call is ambiguous.
However, I'll leave it to real gurus in this area to give an
authoritative answer. If they can also explain the formal rules,
it would be super cool. If I am right, then telling a way
to achieve the same effect directly would be even cooler.
In the meantime, you can emulate ``partial specialization
of function templates'' with classes, for which partial
specialization exists, and static member functions.
Oleg.
--
Life is a sexually transmitted, 100% lethal disease.
[ 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 ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]