473,418 Members | 1,933 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,418 software developers and data experts.

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
4 1837
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: mrstephengross | last post by:
Ok, I've got a class with two template parameters (A and B), and a member function foo(). I want to specialize foo for a particular class A. Is this possible? The following code shows an example: ...
1
by: jn | last post by:
Hi everyone, I was trying to implement a design when I've found what seems a limitation of standard C++. I can't specialize an inner class in this way: template <class ARG> template <>...
3
by: PengYu.UT | last post by:
I have the following two program. The first one compiles well, but the second one doesn't. The only difference between them is one more template parameter is added for the second program. Would...
3
by: toton | last post by:
Hi, I want to specialize template member function of a template class . It is creating some syntax problem .... Can anyone say how to do it ? The class is something like this template<typename...
16
by: PengYu.UT | last post by:
Hi, I want to partial specialize the member function doit. But it doesn't work. Could you please help me to figure out what is wrong? Thanks, Peng template <typename T> class A {
8
by: Rahul | last post by:
Hi, Is there a way to partially specialize only a member function of a template class (not the whole class). e.g. template <typename A, typename B> class Base { public:
7
by: mathieu | last post by:
Hi there, I know this is not possible in c++. So my question, how should I rewrite the following piece of code (without using a dummy class which template parameter could be use for partial...
5
by: huili80 | last post by:
For example, like in the following, the part commented out was intended as partial spectialzation, but it would even compile. Is it even legal to partially specialize a nested template class...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.