471,599 Members | 1,774 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,599 software developers and data experts.

circumventing recursive definition wrt template specialization

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...
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*>

However, the compiler will complain that you're having a
recursive definition, which is illegal...
Obviously, the compiler thinks that myClass<A,T*> is being
defined here, and therefore no other definition exists...
But in this case, myClass<A,T*> is a template specialization,
and therefore there IS another definition, namely the original
template...

So, is there a reason why this isn't possible, or is this just
something that doesn't happen too often, and therefore not worth
extending the standard?

Thanks,

Hans
Jul 19 '05 #1
2 2401
On Tue, 04 Nov 2003 14:29:25 GMT, SainTiss <st***@gmx.net> wrote:
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...
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*>

However, the compiler will complain that you're having a
recursive definition, which is illegal...
Obviously, the compiler thinks that myClass<A,T*> is being
defined here, and therefore no other definition exists...
But in this case, myClass<A,T*> is a template specialization,
and therefore there IS another definition, namely the original
template...
The problem is that specialization is couched in such terms as there
*isn't* an original template. Often the original template wouldn't
make sense or even compile for the type that you are specializing for.
Also, how you you specify that you want to refer to a particular less
specialized version? e.g.

template <class U, class V, class W>
class Foo
{
};

template <class U, class V>
class Foo<U, V, int>
{
};

template <class U, class V>
class Foo<U, int, V>
{
};

template <class U>
class Foo<U, int, int>: public Foo<U, int, int>//which specialization?
{
};

I think it was considered a bit illogical, since a specialization is
by definition the version of the template for the particular
parameters - there is no "original" template, rather a "fall back"
definition. E.g. often this is done:

template <class T>
class Foo; //never defined

//specialisations are defined.
So, is there a reason why this isn't possible, or is this just
something that doesn't happen too often, and therefore not worth
extending the standard?


There's a simple workaround - put the common stuff in a
(implementation only, not interface) base class. To accomodate this
feature, rather than extending the standard, large sections would have
to be rewritten, since it contradicts what is said elsewhere. The
definition as written is reasonably compact and logical, whereas what
this extension would mean in syntax and consistency terms isn't.

Tom
Jul 19 '05 #2
SainTiss wrote in news:9J******************@phobos.telenet-ops.be:
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...
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.
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*>


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/
Jul 19 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by daniel.w.gelder | last post: by
7 posts views Thread by Jon Slaughter | last post: by
reply views Thread by leo001 | last post: by
reply views Thread by Anwar ali | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.