From: Bogdan Jokel on
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.



From: Victor Bazarov on
Bogdan Jokel wrote:
> 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>()

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()

> {
> [...]
> }
>
> 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).

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
From: Bogdan Jokel on
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.


"Victor Bazarov" <v.Abazarov(a)comAcast.net> wrote in message
news:hnm24a$89d$1(a)news.datemas.de...
> Bogdan Jokel wrote:
>> 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>()
>
> 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()
>
>> {
>> [...]
>> }
>>
>> 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).
>
> 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


From: Stephan T. Lavavej [MSFT] on
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

"Bogdan Jokel" <bjokel(a)nospam.com> wrote in message
news:uAhr5LIxKHA.4532(a)TK2MSFTNGP05.phx.gbl...
> 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.
>
>
> "Victor Bazarov" <v.Abazarov(a)comAcast.net> wrote in message
> news:hnm24a$89d$1(a)news.datemas.de...
>> Bogdan Jokel wrote:
>>> 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>()
>>
>> 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()
>>
>>> {
>>> [...]
>>> }
>>>
>>> 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).
>>
>> 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
>
>