Discussion:
Is this partial template specialization possible?
(too old to reply)
Bogdan Jokel
2010-03-15 19:14:32 UTC
Permalink
Hi,

Given the following class template:

template <typename T1, typename T2> class ClassT
{
void foo();
}

template <typename T1, typename T2> void ClassT::foo<T1, T2>()
{
[...]
}

I could specialize foo() method for classes C1 and C2 as:
template <> void ClassT::foo<C1, C2>()
{
[...]
}

Is there a way that I could have a partial specialization for the foo
method? For example, I'd like to have a specialized foo() for class C1 only
(replacing typename T1).

Thanks.
Victor Bazarov
2010-03-15 19:33:59 UTC
Permalink
Post by Bogdan Jokel
Hi,
template <typename T1, typename T2> class ClassT
{
void foo();
}
template <typename T1, typename T2> void ClassT::foo<T1, T2>()
That's incorrect. 'foo' is not a template. 'ClassT' is a template.
The angle brackets must follow the template name, not the member:

template<typename T1, typename T2> void ClassT<T1,T2>::foo()
Post by Bogdan Jokel
{
[...]
}
template <> void ClassT::foo<C1, C2>()
{
[...]
}
Is there a way that I could have a partial specialization for the foo
method? For example, I'd like to have a specialized foo() for class C1 only
(replacing typename T1).
Try the correct syntax, place the template arguments where they belong.
And try dropping the 'template<>' syntax, thus making your
"specialization" not a specialization but a definition:

void ClassT<C1,C2>::foo()
{
.. whatever

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Bogdan Jokel
2010-03-15 21:03:18 UTC
Permalink
Thanks for the reply. I have no issues with compiling my real code. Of
course, the syntax for the template method that I typed into the post was
messed up . I don't even have a good excuse for it :)...

But my question still remains. How can I partially specialize the foo()
method? That is, I'd like to write method implementation for class C1 but
still have T2 parameter. Currently I have a templated method and
specialized one as follows:

template <typename TR, typename TS> void ClassT<TR, TS>::foo()
{
[...]
}

// Specialized method
template <> bool ClassT<C1, C2>::foo()
{
[...]
}

This works fine. But I'm looking for something like the following (please
disregard the syntax):
template <C1, typename TS> void ClassT<C1, TS>::foo()

Is this possible in C++?

Thanks.
Post by Bogdan Jokel
Hi,
template <typename T1, typename T2> class ClassT
{
void foo();
}
template <typename T1, typename T2> void ClassT::foo<T1, T2>()
That's incorrect. 'foo' is not a template. 'ClassT' is a template. The
template<typename T1, typename T2> void ClassT<T1,T2>::foo()
Post by Bogdan Jokel
{
[...]
}
template <> void ClassT::foo<C1, C2>()
{
[...]
}
Is there a way that I could have a partial specialization for the foo
method? For example, I'd like to have a specialized foo() for class C1
only (replacing typename T1).
Try the correct syntax, place the template arguments where they belong.
And try dropping the 'template<>' syntax, thus making your
void ClassT<C1,C2>::foo()
{
.. whatever
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Stephan T. Lavavej [MSFT]
2010-03-15 21:47:07 UTC
Permalink
There's no such thing as partial specialization of function templates. You
can either use a partially specialized helper class (make it a struct with a
static helper function), or an overloaded set of function templates (either
free functions or private member functions of the class you're working on).
The overload resolution that occurs between overloaded function templates
"looks like" partial specialization, even though it isn't. The major
difference between the two techniques is that overloading has to work on
arguments (you would have to introduce dummy T1 *, T2 * arguments or the
like, if your function is truly zero-arg), while partial specialization
doesn't.

Typically I would use a partially specialized helper class here.

STL
Post by Bogdan Jokel
Thanks for the reply. I have no issues with compiling my real code. Of
course, the syntax for the template method that I typed into the post was
messed up . I don't even have a good excuse for it :)...
But my question still remains. How can I partially specialize the foo()
method? That is, I'd like to write method implementation for class C1 but
still have T2 parameter. Currently I have a templated method and
template <typename TR, typename TS> void ClassT<TR, TS>::foo()
{
[...]
}
// Specialized method
template <> bool ClassT<C1, C2>::foo()
{
[...]
}
This works fine. But I'm looking for something like the following (please
template <C1, typename TS> void ClassT<C1, TS>::foo()
Is this possible in C++?
Thanks.
Post by Bogdan Jokel
Hi,
template <typename T1, typename T2> class ClassT
{
void foo();
}
template <typename T1, typename T2> void ClassT::foo<T1, T2>()
That's incorrect. 'foo' is not a template. 'ClassT' is a template. The
template<typename T1, typename T2> void ClassT<T1,T2>::foo()
Post by Bogdan Jokel
{
[...]
}
template <> void ClassT::foo<C1, C2>()
{
[...]
}
Is there a way that I could have a partial specialization for the foo
method? For example, I'd like to have a specialized foo() for class C1
only (replacing typename T1).
Try the correct syntax, place the template arguments where they belong.
And try dropping the 'template<>' syntax, thus making your
void ClassT<C1,C2>::foo()
{
.. whatever
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Loading...