467,921 Members | 1,384 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 467,921 developers. It's quick & easy.

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?

Jul 23 '05 #1
  • viewed: 1632
Share:
4 Replies
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
Jul 23 '05 #2
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).

Jul 23 '05 #3
On 2005-04-07, Victor Bazarov <v.********@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/
Jul 23 '05 #4
Donovan Rebbechi wrote:
On 2005-04-07, Victor Bazarov <v.********@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
Jul 23 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by PengYu.UT | last post: by
7 posts views Thread by mathieu | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.