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

Template specialization

P: n/a
I need help with the following. I feel like I am pretty good at templates
but I can't figure out the following:

I have two templates:

template <int>
struct Mgmt
{
};

template <typename T>
struct MyType
{
};

===========================

Now I want to specialize MyType for Mgmt<int>; it's like a template within a
template but how to do it?
I tried many ways but I have syntax/compiler issues.

One of the examples I have tried:
template <>
struct MyType<Mgmt<int> >
{
};
Thanks for the help.
Jul 19 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
On Mon, 20 Oct 2003 23:52:45 -0500, "sks_cpp" <sk*****@hotmail.com> wrote:
I need help with the following. I feel like I am pretty good at templates
but I can't figure out the following:

I have two templates:

template <int>
struct Mgmt
{
};
This one requires an int value.


template <typename T>
struct MyType
{
};
This one requires a type.


===========================

Now I want to specialize MyType for Mgmt<int>; it's like a template within a
template but how to do it?
I tried many ways but I have syntax/compiler issues.

One of the examples I have tried:
template <>
struct MyType<Mgmt<int> >
{
};


Here you supply a type to MyType (correct), but a type instead of
value to Mgmt (incorrect).
Jul 19 '05 #2

P: n/a
sks_cpp wrote:
I need help with the following. I feel like I am pretty good at templates
but I can't figure out the following:

I have two templates:

template <int>
struct Mgmt
{
};

template <typename T>
struct MyType
{
};

===========================

Now I want to specialize MyType for Mgmt<int>; it's like a template within a
template but how to do it?
I tried many ways but I have syntax/compiler issues.

One of the examples I have tried:
template <>
struct MyType<Mgmt<int> >
{
};


The problem is that Mgmt<int> is not a specialization of
template <int> struct Mgmt
but:

template <> struct Mgmt<101>
is a specialization.

Anyway - this is what I think you meant to do.
#include <iostream>

template <int w_val>
struct Mgmt
{
};

template <typename T>
struct MyType
{
enum { X = 1 };
};
template <int w_val>
struct MyType<Mgmt<w_val> >
{
enum { X = 2 };
};
int main()
{

int regular = MyType< char >::X;
int special = MyType< Mgmt< 101 > >::X;

std::cout << "regular = " << regular << "\n";
std::cout << "special = " << special << "\n";

};

Prints

regular = 1
special = 2

Jul 19 '05 #3

P: n/a
sks_cpp wrote:
I need help with the following. I feel like I am pretty good at templates
but I can't figure out the following:

I have two templates:

template <int>
struct Mgmt
{
};

template <typename T>
struct MyType
{
};

===========================

Now I want to specialize MyType for Mgmt<int>; it's like a template within a
template but how to do it?


I doesn't appear to make sense to me. Your 'Mgmt' template is
parametrized with a non-type parameter of type 'int'. Specializations of
'Mgmt' might include 'Mgmt<0>', 'Mgmt<10>' etc. but not 'Mgmt<int>'.
'Mgmt<int>' is illegal given the above declaration of 'Mgmt'. Think
again about what exactly you are trying to do.

--
Best regards,
Andrey Tarasevich
Brainbench C and C++ Programming MVP

Jul 19 '05 #4

P: n/a
"Gianni Mariani" <gi*******@mariani.ws> wrote in message
news:bn********@dispatch.concentric.net...
sks_cpp wrote:
I need help with the following. I feel like I am pretty good at templates but I can't figure out the following:

I have two templates:

template <int>
struct Mgmt
{
};

template <typename T>
struct MyType
{
};

===========================

Now I want to specialize MyType for Mgmt<int>; it's like a template within a template but how to do it?
I tried many ways but I have syntax/compiler issues.

One of the examples I have tried:
template <>
struct MyType<Mgmt<int> >
{
};


The problem is that Mgmt<int> is not a specialization of
template <int> struct Mgmt
but:

template <> struct Mgmt<101>
is a specialization.

Anyway - this is what I think you meant to do.
#include <iostream>

template <int w_val>
struct Mgmt
{
};

template <typename T>
struct MyType
{
enum { X = 1 };
};
template <int w_val>
struct MyType<Mgmt<w_val> >
{
enum { X = 2 };
};
int main()
{

int regular = MyType< char >::X;
int special = MyType< Mgmt< 101 > >::X;

std::cout << "regular = " << regular << "\n";
std::cout << "special = " << special << "\n";

};

Prints

regular = 1
special = 2


From reading the other responses, I realize that I did a poor job of
explaining what I am trying to do.
However, you hit it right on the head - this is exactly what I need. Thank
you very much.

Jul 19 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.