Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Partial template specialization

Reply
Thread Tools

Partial template specialization

 
 
Hicham Mouline
Guest
Posts: n/a
 
      09-24-2007
Hi

I have a class template Foo with 10 non-static member methods.
Foo needs to be partially specialized because the implementation of some
(perhaps 2 or 3) of the methods in
the primary template is not appropriate for the specialized template.

I made the partially specialized template inherit from the primary template
in order to use the primary implementations
for the 7 methods and the specialized implementations for the other 3
methods.
All the methods of the primary template, that is the base class, are now
non-pure virtual.

Other ways to do this?

rds,


 
Reply With Quote
 
 
 
 
werasm
Guest
Posts: n/a
 
      09-24-2007

Hicham Mouline wrote:
> Hi
>
> I have a class template Foo with 10 non-static member methods.
> Foo needs to be partially specialized because the implementation of some
> (perhaps 2 or 3) of the methods in
> the primary template is not appropriate for the specialized template.


If they are not appropriate, how could you inherit from the primary
template? Also, if you inherit from the primary template from a
specialization, obviously you need to change your template params,
right, otherwise you instantiate the template recursively.

Perhaps post your code, or small example.

Werner

 
Reply With Quote
 
 
 
 
Hicham Mouline
Guest
Posts: n/a
 
      09-24-2007

"werasm" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) ups.com...
>
> Hicham Mouline wrote:
>> Hi
>>
>> I have a class template Foo with 10 non-static member methods.
>> Foo needs to be partially specialized because the implementation of some
>> (perhaps 2 or 3) of the methods in
>> the primary template is not appropriate for the specialized template.

>
> If they are not appropriate, how could you inherit from the primary
> template? Also, if you inherit from the primary template from a
> specialization, obviously you need to change your template params,
> right, otherwise you instantiate the template recursively.
>
> Perhaps post your code, or small example.
>
> Werner
>

I was wrong.
Basically this is what i need:

template <typename A, const int n>
class Foo {
f1() .... f10();
};

// Partial specialization for n=2
// just f2() and f3() from the primary template are not good for the case
n=2,
// so i need to "override" them, but only those 2
// i wish not to redefine the other methods
template <typename A>
class Foo<A,2> : public Foo {
f1() .... f10();
};



 
Reply With Quote
 
werasm
Guest
Posts: n/a
 
      09-24-2007

Hicham Mouline wrote:

>
> I was wrong.
> Basically this is what i need:
>
> template <typename A, const int n>
> class Foo {
> f1() .... f10();
> };
>
> // Partial specialization for n=2
> // just f2() and f3() from the primary template are not good for the case
> n=2,
> // so i need to "override" them, but only those 2
> // i wish not to redefine the other methods
> template <typename A>
> class Foo<A,2> : public Foo {
> f1() .... f10();
> };



A quick structure that comes to mind (although I haven't given this
that much thought) is:

1) Create a base class per function, and specialize where required.

2) Create a common data component that represents your private
data. This you could store as reference in each of your bases, the
real data living in the derived (privately). Alternatively you could
use a virtual
base for this purpose. I prefer the reference though, but it may not
be touched during destruction (which makes sense as it may only
be touched in the function that it represents, right?

3) Derive from each of the bases. If N=2, for the cases where it
matters
the specialized base would automatically become the actual base. All
the bases form part of the interface, and the Derived (Concrete)
basically
consist of a constructor and private data only. Get it?

Regards,

Werner

 
Reply With Quote
 
werasm
Guest
Posts: n/a
 
      09-24-2007
Hicham Mouline wrote:


> I was wrong.
> Basically this is what i need:

[snip]
I hope I understood you correct. If so, this
is what I had in mind. This allows you to
specialize, whereupon the method is replaced
by the specialized. There are other ways
in which you could share data as well. Boost's
base_from_member idiom comes to mind.

Here goes (It does not link as you have
to implement do1() and do2()!):

template <class T>
struct FooData
{
public:
FooData( T val )
: value_(val){ }
protected:
FooData(){ }
T value_;
};

template <class T, int N>
struct FooFun1
: virtual FooData<T>{ void do1(); };

template <class T, int N>
struct FooFun2
: virtual FooData<T>{ void do2(); };

template <class T>
struct FooFun2<T,2>
: virtual public FooData<T>{ void do2(); };

template <class T, int N>
class Foo
: public FooFun1<T,N>,
public FooFun2<T,N>
{
public:
Foo( T data )
: FooData<T>( data ),
value_( FooData<T>::value_ )
{ }
private:
//Hides base protected member.
// Not really used.
T& value_;
};

int main()
{
Foo<int,2> test( 20 );
test.do1();
test.do2();//Specialization called.
return 0;
}

The hiding of the name value_ is not necessary,
as it is not the intention of anybody to derive
from Foo anyway. I just put it there for the
bureaucrats .

Regards,

Werner

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Template template partial specialization Hizo C++ 17 03-07-2011 08:09 AM
how to do partial specialization template for non member method template vj C++ 1 12-20-2010 11:57 AM
syntax problem for partial template specialization for a template class. toton C++ 1 12-28-2006 04:08 PM
template specialization overriding non-specialization? Joseph Turian C++ 2 04-16-2006 02:46 PM
template partial specialization Chandra Shekhar Kumar C++ 17 06-25-2003 03:35 PM



Advertisments