By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,407 Members | 920 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,407 IT Pros & Developers. It's quick & easy.

Partially specialize a template with another template class

P: n/a
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
Share this Question
Share on Google+
4 Replies


P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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.