By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,890 Members | 1,285 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,890 IT Pros & Developers. It's quick & easy.

Specialization of only one template parameter?

P: n/a
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
Share this Question
Share on Google+
1 Reply


P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.