SainTiss wrote in news:9JOpb.2857$NU.187538@phobos.telenet-ops.be:
[color=blue]
> Hi,
>
> If you've got a template class with lots of methods,
> and then you've got a type which works with the template,
> except for one method...
>
> What you need to do there is specialize the template for your
> type. However, this requires you to copy the whole template, and
> change the method, which leads to code duplication...[/color]
Here's another common way of doing it:
#include <iostream>
namespace detail
{
template < typename U, typename V >
struct basic_method_helper;
}
template < typename U, typename V >
struct basic
{
typename
detail::basic_method_helper< U, V >::return_type
method( V & v )
{
return detail::basic_method_helper< U, V >::apply( this, v );
}
V multiply;
basic( V f ): multiply( f ) {}
};
namespace detail
{
template < typename U, typename V >
struct basic_method_helper
{
// non-specialized versions
typedef U return_type;
static return_type apply( basic< U, V > *that, V & v )
{
return U(v * that->multiply);
}
};
template < typename U >
struct basic_method_helper< U, U >
{
// partialy specialized version
typedef U return_type;
static return_type apply( basic< U, U > *that, U & v )
{
return v * that->multiply;
}
};
}
int main()
{
double d = 2.5;
basic< int, double > bid( 3.5 );
basic< double, double > bdd ( 7 );
std::cerr << bid.method( d ) << "\n";
std::cerr << bdd.method( d ) << "\n";
}
Its a fair bit of typing, but more maintainable than copying a
whole class.
[color=blue]
> If there's only 1 template parameter, one can specialize the method
> instead of the whole class, but with more template parameters,
> this is impossible, because partial specialization of a
> method isn't allowed...
>
> Therefore, it would be handy to be able to do this:
> template<class A, class T> myClass<A,T*> : public myClass<A,T*>
>[/color]
Well why not just allow partial specialization of a function's,
member and non-member. I've read talk that a future standard
might allow this, but wether this would extend to member function's
I don't know (but I can't see why not).
If you want to talk about the why's of the current standard and
it's future direction's comp.std.c++ maybe better than here.
[snip]
Rob.
--
http://www.victim-prime.dsl.pipex.com/