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