473,722 Members | 2,458 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2539
On Tue, 04 Nov 2003 14:29:25 GMT, SainTiss <st***@gmx.ne t> 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<cla ss 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******** **********@phob os.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_he lper;

}

template < typename U, typename V >
struct basic
{
typename
detail::basic_m ethod_helper< U, V >::return_typ e
method( V & v )
{
return detail::basic_m ethod_helper< U, V >::apply( this, v );
}
V multiply;
basic( V f ): multiply( f ) {}
};

namespace detail
{
template < typename U, typename V >
struct basic_method_he lper
{
// 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_he lper< 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
6193
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 general function template... template <class T>
3
1953
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
1510
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> LongAndBoolCoolness;
7
567
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
6283
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 to illustrate. template<typename T> class C { public:
9
1661
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 <iostream> template <typename T>
9
2780
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 assign() function really a specialization of the first assign() or is it an assign() overload? Thank you. -- Marek
3
2486
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
2983
by: er | last post by:
hi, could someone please help with this code? template<unsigned int N,unsigned int M> class A{ public: A(); };
0
8867
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8740
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8059
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5996
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4503
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4764
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3208
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2606
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2148
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.