473,835 Members | 1,851 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 4173
On Mon, 20 Oct 2003 23:52:45 -0500, "sks_cpp" <sk*****@hotmai l.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_v al> >
{
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*******@mari ani.ws> wrote in message
news:bn******** @dispatch.conce ntric.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_v al> >
{
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
6870
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 18.3.1, 'Iseq'). I want the version for containers that he gives, but also to provide a specialization for construction from a pair<It,It> (eg because that is returned by equal_range()).
2
2545
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 template for your type. However, this requires you to copy the whole template, and change the method, which leads to code duplication... If there's only 1 template parameter, one can specialize the method
8
7692
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 specialize Music<Bach>, I expect that the original play() method is available in the specialization, but it is not. How can I fix this? -X
2
5782
by: Jeff | last post by:
/* -------------------------------------------------------------------------- Hello, I was experimenting with class templates and specializing member functions and came across a simple problem which I can't figure out. I have a simple class C with 2 template member objects, and a function print() that prints the value of these objects. I defined a specialization of print() for C<string, char> which is called correctly. Then I wanted...
6
2023
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 instantiation of foo:
4
1969
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
2790
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 assign() function really a specialization of the first assign() or is it an assign() overload? Thank you. -- Marek
2
2499
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: VertexValueTyp& vertex(int i); that dereferences vertices internally in case VertexTyp is a pointer
2
7706
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
2623
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 Indexer<std::vector<T,A {} matches only with nonconst version. anyway to match it for both? and get if it is const or nonconst? Actually i want 2 specialization, one for std::vector<T,Aconst & non const other for std::deque<T,Aconst & non const.
0
9803
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9652
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10811
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10523
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9345
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5636
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4434
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3993
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3088
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.