Jeff wrote in news:7b01eaf3.0311191039.34fcfa1d@posting.google.c om:
[color=blue]
> 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 to define a specialization of print() that
> would be called for C<class T, int>, where T is any type. I couldn't
> get this code to compile, let alone run, and tried various ways to
> make it work. The full source is below.
>[/color]
Your problem is that you are trying to create a partial specialization
of a function (non-static member in this case, but that doesn't matter).
For some reason this is not allowed by the language (possibly the
standards committee thought overloading was enough). The workaround
as always is to add another layey of indirection (see below).
[color=blue]
> Is there a nice solution to this problem? I read somewhere (can't
> recall where) that you can't specialize member functions for classes
> without specializing the whole class, and yet the C<string,
> char>::print() specialization works.[/color]
Yup explicit specialization's (template <>) are allowed.
[color=blue]
>[/color]
#include <iostream>
#include <typeinfo>
struct example
{
template < typename U, typename V >
void patialy_specialized( U const &u, V const &v );
};
/* helper class
*/
template < typename U, typename V >
struct example_patialy_specialized
{
static void apply( example * that, U const &u, V const &v )
{
std::cerr
<< "default version "
<< typeid( U ).name()
<< " - "
<< typeid( V ).name()
<< "\n"
;
}
};
/* defenition of template method, indirects through the
"helper" above or (importantly) one of its specializations.
*/
template < typename U, typename V >
inline void example::patialy_specialized( U const &u, V const &v)
{
return example_patialy_specialized< U, V >::apply( this, u, v );
}
/* demo specialization of the "helper"
*/
template < typename U >
struct example_patialy_specialized< U, int >
{
static void apply( example * that, U const &arg, int )
{
std::cerr << "U int version " << typeid( U ).name() << "\n";
}
};
int main()
{
example ex;
double d = 1;
ex.patialy_specialized( d, d ); // default
ex.patialy_specialized( d, 1 ); // partial specialization
}
HTH
Rob.
--
http://www.victim-prime.dsl.pipex.com/