partial member specialization

T

Thomas Maier-Komor

Hi everybody,

I am trying to figure out, how to define a partial member
specialization... Is this possible? What is the syntax
for this?


I have:

template <class A, class B>
class T
{
public:
T(void);

/* and some more functions which are not relevant here
but should be identical in the unspecialized and
specialized template classes
*/
};

and I would like to do a specialization for the constructor
of T. This constructor should have two arguments (e.g.
T(int,int) ) and the specialization should only be valid
if template argument A is a certain predefined class.

Any ideas? I have been searching Vandervoorde/Josuttis
C++ Templates with no success...


Thanks,

Tom
 
V

Victor Bazarov

Thomas said:
I am trying to figure out, how to define a partial member
specialization... Is this possible?

No. Your member is not a template. Your class is. So all you
can [partially] specialise is the class itself.
What is the syntax
for this?

template< ? > class T< ??? > { ...

The ? and ??? should contain the specialisation arguments.
I have:

template <class A, class B>
class T
{
public:
T(void);

/* and some more functions which are not relevant here
but should be identical in the unspecialized and
specialized template classes
*/
};

and I would like to do a specialization for the constructor
of T. This constructor should have two arguments (e.g.
T(int,int) ) and the specialization should only be valid
if template argument A is a certain predefined class.

You need to [partially] specialise your class then.

template<class U> class T<CertainPredefinedClass, U> {
public:
T(int, int);
/* and other functions which are not relevant here
but will have to be repeated because they are not
automatically copied from the original template
*/
};
Any ideas? I have been searching Vandervoorde/Josuttis
C++ Templates with no success...

Wow... Really? They don't talk about partial specialisation of class
templates there? At all? And I thought it was a useful book... Damn!

V
 
M

Malte Starostik

Thomas said:
Hi everybody,

I am trying to figure out, how to define a partial member
specialization... Is this possible? What is the syntax
for this?


I have:

template <class A, class B>
class T
{
public:
T(void);

/* and some more functions which are not relevant here
but should be identical in the unspecialized and
specialized template classes
*/
};

and I would like to do a specialization for the constructor
of T. This constructor should have two arguments (e.g.
T(int,int) ) and the specialization should only be valid
if template argument A is a certain predefined class.

Basically what Victor said... to avoid having to copy & paste all the
common stuff:

template< class A, class B >
class TBase
{
// common members here
};

template< class A, class B >
class T : public TBase< A, B >
{
};

Partial specialization for A == B:

template< class A >
class T< A, A > : public TBase< A, A >
{
public:
T(); // T(void); is C-ish
};

HTH,
Malte
 
V

Victor Bazarov

Thomas said:
[..]
Victor - you are right. It is a damn useful book and I learned a great
deal reading it. But look at the TOC - they talk about:
- partial class template specialization (sec 12.4)
- full class template specilization (sec 12.3.1)
- full function template specilization (sec 12.3.2)
- full member specilization (sec 12.3.3)

I was looking for something like partial member specialization, after
reading these sections. But Malte is probably right, when suggesting
inheritance to add a new constructor for a special case instead
of doing some weird and probably inpossible specilization... And you
are right that I am actually having a class template and not a
member template.

Well, the reason you couldn't find a partial member specialisation is
quite simple. There are no partial specialisations for function
templates in C++. That's why Josuttis and Vandevoorde didn't include
that in the TOC (and don't talk about), I suspect.

Even if you did have

class A {
template<class T> void foo(T);
};

you couldn't do

template<class T> void A::foo<T*>(T*);

because the language does not allow that. IIRC, of course.

Victor
 
T

Thomas Maier-Komor

Victor said:
Wow... Really? They don't talk about partial specialisation of class
templates there? At all? And I thought it was a useful book... Damn!

V

First of all thank you Victor Bazarov and especially to Malte Starostik.

Victor - you are right. It is a damn useful book and I learned a great
deal reading it. But look at the TOC - they talk about:
- partial class template specialization (sec 12.4)
- full class template specilization (sec 12.3.1)
- full function template specilization (sec 12.3.2)
- full member specilization (sec 12.3.3)

I was looking for something like partial member specialization, after
reading these sections. But Malte is probably right, when suggesting
inheritance to add a new constructor for a special case instead
of doing some weird and probably inpossible specilization... And you
are right that I am actually having a class template and not a
member template.

Cheers,

Tom
 
T

Thomas Maier-Komor

Victor said:
Well, the reason you couldn't find a partial member specialisation is
quite simple. There are no partial specialisations for function
templates in C++. That's why Josuttis and Vandevoorde didn't include
that in the TOC (and don't talk about), I suspect.

Even if you did have

class A {
template<class T> void foo(T);
};

you couldn't do

template<class T> void A::foo<T*>(T*);

because the language does not allow that. IIRC, of course.

Victor

Well, I guess it is about time for me getting the standard
and start reading it, in addition to the C++ Templates book.
Maybe it would be also a good idea getting another cup of
coffee before working on tricky stuff in the future... ;-)

Tom
 

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

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top