473,325 Members | 2,671 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,325 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 4084
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: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.