473,398 Members | 2,343 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,398 software developers and data experts.

Template specialization

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

Similar topics

17
by: Paul MG | last post by:
Hi Template partial specialization always seems like a fairly straightforward concept - until I try to do it :). I am trying to implement the input sequence type (from Stroustrup section...
2
by: SainTiss | last post by:
Hi, If you've got a template class with lots of methods, and then you've got a type which works with the template, except for one method... What you need to do there is specialize the...
8
by: Agent Mulder | last post by:
Hi group, I have a problem with partial template specialization. In the code below I have a template struct Music with one method, play(), and three kinds of music, Jazz, Funk and Bach. When I...
2
by: Jeff | last post by:
/* -------------------------------------------------------------------------- Hello, I was experimenting with class templates and specializing member functions and came across a simple problem...
6
by: Dave | last post by:
Hello all, Consider this function template definition: template<typename T> void foo(T) {} If foo is never called, this template will never be instantiated. Now consider this explicit...
4
by: TT \(Tom Tempelaere\) | last post by:
Comeau compiler complains (too few arguments for class template "B") at line *** #include <memory> template<typename T, size_t n> struct A {}; template<typename T, size_t n> struct B;
9
by: Marek Vondrak | last post by:
Hello. I have written the following program and am curious why it prints "1" "2". What are the exact effects of explicitly providing function template parameters at the call? Is the second...
2
by: Thomas Kowalski | last post by:
Hi, I would like to write a template class Polygon<VertexTypthere vertex typ can be eigther a pointer or a value typ. It has an attribute: std::vector<VertexTypvertices; And a methode:...
2
by: Barry | last post by:
The following code compiles with VC8 but fails to compiles with Comeau online, I locate the standard here: An explicit specialization of any of the following:
6
by: abir | last post by:
i have a template as shown template<typename Sclass Indexer{}; i want to have a specialization for std::vector both const & non const version. template<typename T,typename Aclass...
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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
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...

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.