Partial template specialization

H

Hicham Mouline

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,
 
W

werasm

Hicham said:
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
 
H

Hicham Mouline

werasm said:
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();
};
 
W

werasm

Hicham said:
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
 
W

werasm

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top