Partially specialize a template with another template class

O

Old Wolf

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?
 
V

Victor Bazarov

Old said:
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
 
O

Old Wolf

Victor said:
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).
 
D

Donovan Rebbechi

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

Victor Bazarov

Donovan said:
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
 

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,053
Latest member
BrodieSola

Latest Threads

Top