Re: partial partial template specialization?
On 23 Mai, 04:41, rbin...@comcast.net wrote:
Have I transferred from the realm of "I can't figure out the syntax"
to the realm of "the language does not allow it"?
I am trying to specialize the following template class.
template <typename T, int min =0, int max =0> class Foo
{
private:
T t;
public:
//...
operator T() const { return T(t); };
//...
};
Specifically I want to specialize the cast operator for <char> with
any min/max.
template<> Foo< char >::operator char() const; // {return int(t);};
This is an explicit specialization, where no template parameters
remain
unresolved.
I get the "overridden" behavior only for variables declared with
default min/max: e.g.
Foo<char> f;
cout << f << endl; // desired "overridden" behavior
Foo<char,5> ff;
cout << ff << endl; //default unspecialized behavior
I figured it would follow the partial specialization syntax; e.g.
template <int min, int max> Foo<char, min, max>::operator char()
const;
but I can not get anything resembling this to compile.
Thoughts?
Obviously you don't want to partially specialize Foo, just
only the implementation of it's operator T(). Your problem is,
that function templates cannot be partially specialized.
But there is hope in sight: Your problem can be easily solved
via one indirection such that the conversio operator uses
another class template, e.g. like this:
template <typename T, int min =0, int max =0>
class Foo
{
private:
T t;
template <typename U, int min2, int max2>
friend class Converter;
public:
inline operator T() const;
};
template <typename T, int min, int max>
class Converter {
public:
static T doConvert(const Foo<T, min, max>& f) {
return T(f.t);
}
};
template <typename T, int min, int max>
inline Foo<T, min, max>::operator T() const {
return Converter<T, min, max>::doConvert(*this);
}
template <int min, int max>
class Converter<char, min, max> {
public:
static char doConvert(const Foo<char, min, max>& f) {
return int(f.t);
}
};
HTH & Greetings from Bremen,
Daniel Kr?gler
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]