Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C++ (http://www.velocityreviews.com/forums/f39-c.html)
-   -   Partially specialize a template with another template class (http://www.velocityreviews.com/forums/t290026-partially-specialize-a-template-with-another-template-class.html)

Old Wolf 04-07-2005 10:49 PM

Partially specialize a template with another template class
 
I have a template class like this:

template<typename T, int N>
struct Foo
{
...........
};

which I have successfully specialized for some types, eg:

template<int N>
struct Foo<std::string, N>
{ ...... }

Is it possible to specialize it so that T is a std::pair<A,B>
where A and B are new template parameters?


Victor Bazarov 04-07-2005 11:49 PM

Re: Partially specialize a template with another template class
 
Old Wolf wrote:
> I have a template class like this:
>
> template<typename T, int N>
> struct Foo
> {
> ...........
> };
>
> which I have successfully specialized for some types, eg:
>
> template<int N>
> struct Foo<std::string, N>
> { ...... }
>
> Is it possible to specialize it so that T is a std::pair<A,B>
> where A and B are new template parameters?


And where would they come from? If neither A nor B are among the
original template's arguments, how is it "specializing"?

V



Old Wolf 04-08-2005 01:51 AM

Re: Partially specialize a template with another template class
 
Victor Bazarov wrote:
> Old Wolf wrote:
> > I have a template class like this:
> >
> > template<typename T, int N>
> > struct Foo
> > {
> > ...........
> > };
> >
> > which I have successfully specialized for some types, eg:
> >
> > template<int N>
> > struct Foo<std::string, N>
> > { ...... }
> >
> > Is it possible to specialize it so that T is a std::pair<A,B>
> > where A and B are new template parameters?

>
> And where would they come from?


That's what I was asking

> If neither A nor B are among the original template's arguments,
> how is it "specializing"?


The original struct works for any type, the specialization
I want works for a std::pair (which is a subset of all types).


Donovan Rebbechi 04-08-2005 02:38 AM

Re: Partially specialize a template with another template class
 
On 2005-04-07, Victor Bazarov <v.Abazarov@comAcast.net> wrote:
> Old Wolf wrote:
>> I have a template class like this:
>>
>> template<typename T, int N>
>> struct Foo
>> {
>> ...........
>> };
>>
>> which I have successfully specialized for some types, eg:
>>
>> template<int N>
>> struct Foo<std::string, N>
>> { ...... }
>>
>> Is it possible to specialize it so that T is a std::pair<A,B>
>> where A and B are new template parameters?

>
> And where would they come from? If neither A nor B are among the
> original template's arguments, how is it "specializing"?


Maybe he means something like the following. It's specializing, because
std::pair<A,B> is the first argument to the template.

This compiles with gcc 3.3.5 with standards options turned up. But is it
correct ?

#include <utility>
#include <string>
template<typename T, int N> struct Foo { };
template<int N> struct Foo<std::string, N> { };

template <typename A, typename B>
struct Foo< std::pair<A,B>, 3> { void f() {} };

int main() { Foo<std::pair<int,double>, 3> x; x.f(); }

Cheers,
--
Donovan Rebbechi
http://pegasus.rutgers.edu/~elflord/

Victor Bazarov 04-08-2005 03:53 PM

Re: Partially specialize a template with another template class
 
Donovan Rebbechi wrote:
> On 2005-04-07, Victor Bazarov <v.Abazarov@comAcast.net> wrote:
>
>>Old Wolf wrote:
>>
>>>I have a template class like this:
>>>
>>> template<typename T, int N>
>>> struct Foo
>>> {
>>> ...........
>>> };
>>>
>>>which I have successfully specialized for some types, eg:
>>>
>>> template<int N>
>>> struct Foo<std::string, N>
>>> { ...... }
>>>
>>>Is it possible to specialize it so that T is a std::pair<A,B>
>>>where A and B are new template parameters?

>>
>>And where would they come from? If neither A nor B are among the
>>original template's arguments, how is it "specializing"?

>
>
> Maybe he means something like the following. It's specializing, because
> std::pair<A,B> is the first argument to the template.
>
> This compiles with gcc 3.3.5 with standards options turned up. But is it
> correct ?
>
> #include <utility>
> #include <string>
> template<typename T, int N> struct Foo { };
> template<int N> struct Foo<std::string, N> { };
>
> template <typename A, typename B>
> struct Foo< std::pair<A,B>, 3> { void f() {} };
>
> int main() { Foo<std::pair<int,double>, 3> x; x.f(); }


That makes sense... I even tried

template<class A, class B, int N> struct Foo<std::pair<A,B>,N> ...

and it was OK. My misunderstanding was simple: I thought that you are
only allowed to shorten the list of arguments or change them slightly
(as in T to T*, for example). Here we actually add arguments or totally
replace them, which is kind of weird, I guess I am just not used to them.

V


All times are GMT. The time now is 06:48 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.