Afficher un message
Vieux 07/02/2008, 21h09   #6
Andrey Tarasevich
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: specialization of member template

mark.mac@gmail.com wrote:
> ...
> I couldn't figure out how to specialize the following member template:
>
> #include <iostream>
> template <typename T1> class sample {
> T1 value;
>
> public:
> sample(T1 v) : value(v) {}
> template <typename T2> T1 val(T2 arg);
> };
>
> template <typename T1> template <typename T2> T1 sample<T1>::val(T2 arg)
> {
> return arg + value;
> }
>
> template <typename T1> template <> T1 sample<T1>::val(char arg)
> {
> return arg + value;
> }
> ...
> I only want to partial specialize with T2 = char. Above gives error
> "enclosing class templates are not explicitly specialized"
>
> How do I fix this? Thanks.


In C++ for some reason explicit specialization of member templates is
only allowed when the enclosing class template is also explicitly
specialized.

For example, the following code is ill-formed for exactly that reason

template<typename T> struct A {
template<typename U> struct B { int i; };
template<> struct B<int> { int j; }; // <- ERROR
};

With class templates there's an [ugly] workaround: partial
specialization of member templates are perfectly OK, even without
specializing the enclosing class, so the above can be turned into a
partial specialization by introducing a dummy parameter

template<typename T> struct A {
template<typename U, typename DUMMY = void> struct B { int i; };
template<typename DUMMY> struct B<int, DUMMY> { int i; }; // <- OK
};

...
A<void>::B<char> bc;
A<void>::B<int> bi;

bc.i = 4;
bi.j = 2;

It is not exactly the same thing, but it s in many cases (which
makes the aforementioned restriction on explicit specialization look
even more illogical).

In your case though we are dealing with a member function template, not
a member class template. Function templates don't support partial
specialization, which means that we can't use the above workaround.
However, I think you can achieve [almost?] what you want by using a
different C++ mechanism - function overloading. Just supply an
overloaded version of your function with 'char' argument and you should
be OK. Of course, you'll have to add its declaration to the template
class definition

template<typename T1> class sample {
T1 value;

public:
sample(T1 v) : value(v) {}
template<typename T2> T1 val(T2 arg);
T1 val(char arg);
};

template<typename T1> template<typename T2> T1 sample<T1>::val(T2 arg)
{
return arg + value;
}

template<typename T1> T1 sample<T1>::val(char arg)
{
return arg + value;
}

In contexts when member template arguments are not specified, the
compiler will select the overloaded function.

sample<int> s;
s.val('c'); // calls the overloaded 'char' version

But keep in mind that when the template arguments are specified
explicitly (or a mere '<>' is used), the template version will be chosen

s.val<>('c'); // instantiates and calls the template version
s.val<char>('c'); // instantiates and calls the template version

In this respect it is different from explicit specialization. I don't
know whether this matters in your case.

--
Best regards,
Andrey Tarasevich
  Réponse avec citation
 
Page generated in 0,06857 seconds with 9 queries