473,385 Members | 1,445 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 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 2511
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Hartmut Sbosny | last post by:
Hello NG, I have a question. I have a header file with a function template and a fully specialized version of it, for instance //======== File "templ.hpp" ======== #include <iostream> // the...
3
by: BigMan | last post by:
Why cannot I define a member of an explicitly specialized class template out of the class template specialization: template< int i > struct a { static void Do( ); }; template< >
1
by: daniel.w.gelder | last post by:
Hi. I have a template with 3 params: template <typename BASE, typename SUPER=void, typename SUPER2=void> Coolness { }; typedef Coolness<long> LongCoolness; typedef Coolness<long, bool>...
7
by: Jon Slaughter | last post by:
#pragma once #include <vector> class empty_class { }; template <int _I, int _J, class _element, class _property> class RDES_T {
1
by: Michael Stembera | last post by:
I have a case of a very simple template class w/ a template method. If I define a specialization of the method outside the body of the template class it does not compile. Here is a tiny example...
9
by: ma740988 | last post by:
I'm following section 35 of the FAQ and am I'm trying to do some specialization. Trouble is I have a recursive problem here that I'm not sure how to solve. Ideas? The snippet: # include...
9
by: Marek Vondrak | last post by:
Hello. I have written the following program and am curious why it prints "1" "2". What are the exact effects of explicitly providing function template parameters at the call? Is the second...
3
by: IR | last post by:
Hi, I've been trying to do the following (which doesn't compile) : template<class T, class F = Example<T struct Example { F foo(); };
7
by: er | last post by:
hi, could someone please help with this code? template<unsigned int N,unsigned int M> class A{ public: A(); };
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

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.