Template specialization

S

sks_cpp

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.
 
A

Alf P. Steinbach

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).
 
G

Gianni Mariani

sks_cpp said:
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
 
A

Andrey Tarasevich

sks_cpp said:
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.
 
S

sks_cpp

Gianni Mariani said:
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.
 

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
474,145
Messages
2,570,825
Members
47,371
Latest member
Brkaa

Latest Threads

Top