473,473 Members | 2,303 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Specialization of only one template parameter?

How can I specialize the value of only one template parameter?

Here's the fragment of code I'd like to get working:

typedef enum {START, PARENT, CHILD, END} locator_ty;
template <locator_ty L, bool Signed>
class foo {
string str();
...
};

template <bool Signed> string foo<START, Signed>::str() const { return
"start"; }
template <bool Signed> string foo<END, Signed>::str() const { return
"end"; }
template <bool Signed> string foo<PARENT, Signed>::str() const { return
"parent"; }
template <bool Signed> string foo<CHILD, Signed>::str() const { return
"child"; }
Except the last four lines aren't syntactically correct.
Can anyone tell me the correct syntax?
Thanks!

Joseph

Jan 5 '06 #1
1 1480
Joseph Turian wrote:
How can I specialize the value of only one template parameter?

Here's the fragment of code I'd like to get working:

typedef enum {START, PARENT, CHILD, END} locator_ty;
template <locator_ty L, bool Signed>
class foo {
string str();
...
};

template <bool Signed> string foo<START, Signed>::str() const { return
"start"; }
template <bool Signed> string foo<END, Signed>::str() const { return
"end"; }
template <bool Signed> string foo<PARENT, Signed>::str() const { return
"parent"; }
template <bool Signed> string foo<CHILD, Signed>::str() const { return
"child"; }
Except the last four lines aren't syntactically correct.
Can anyone tell me the correct syntax?


There is none. What you're trying to do is to have a partial
specialisation of a function template. There is no such thing in C++.
You need to partially specialise the whole class. Example:

template<bool Signed>
class foo<END, Signed> {
string str() { return "end"; }
...
};

of course it may not be what you want because you need to re-implement all
the members there. You need to redesign. Why couldn't you, for example,
simply have 'str' member call another function, fully specialised, or that
has one argument:

string external_str(locator_ty L)
{
switch (L) {
case START: return "start";
case END: return "end";
}
}

template<locator_ty L> string ex_str(); // declaration

template<> string ex_str<START>() { return "start"; } // spec'tion
template<> string ex_str<END>() { return "end"; } // spec'tion
template<locator_ty L, bool Signed>
class foo {
string str() { return external_str(L); }
or
string str() { return ex_str<L>(); }
...
};
?

V
Jan 5 '06 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

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...
5
by: Andy | last post by:
Hi, I got a little confused on 'instantiation' and 'specialization', espcially for explicit instantiation and explicit sepcialization. Can anybody explain the difference? Thanks a lot! ...
8
by: Ferdi Smit | last post by:
I've never understood the rationale of allowing partial, but not explicit specialization for classes at non-namespace scope. Ie.: struct A { template <typename T1, typename T2> struct B {}; ...
6
by: Kalle Rutanen | last post by:
Here is a short code. I can't see why the template specialization does not work (vsnet2003)? Does the standard allow these kind of specializations? template <int i> class B {}; template...
1
by: Kai-Uwe Bux | last post by:
Hi folks, I would like to know which clause of the standard rules out this: template < typename eval > struct recursive_template { typedef typename eval::enum_type Enum;
2
by: Imre | last post by:
Hi I'd like to know what the problem is with the following code. I've tried compiling it with two compilers (VC++ 8.0, and Comeau online compiler), and both failed to compile it, saying that...
2
by: Nick | last post by:
I'm learning C++ and ran into a compile error using Visual C++ 2005 Express on the following example program (located at http://www.cplusplus.com/doc/tutorial/templates.html): // template...
8
by: flopbucket | last post by:
Hi, I want to provide a specialization of a class for any type T that is a std::map. template<typename T> class Foo { // ... };
8
by: Konstantin | last post by:
I have two possible implementations of my class: template <typename T> class MyContainer { public: typedef typename std::set<T>::const_iterator const_iterator; void add( T id ) {...
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
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,...
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...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
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...
1
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...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
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 ...

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.