Re: template class specialization trouble
On 7 Apr., 18:13, Road Dog <no...@nowhere.com> wrote:
This seems to work fine:
template <typename A, typename B, typename C>
class D
{
};
template <typename A, typename C>
class D<A, void, C>
{
};
but this:
template <typename A, typename B, void (A::*MF)(B)>
class D
{
};
template <typename A, void (A::*MF)()>
class D<A, void, MF>
class D
{
};
produces:
test4.cc:17: error: invalid parameter type 'void'
test4.cc:12: error: in declaration 'template<class T, class W, void
(T::* MF)(W)> struct Whatever'
test4.cc:17: error: '<type error>' is not a valid type for a template
constant parameter
This:
class C<A, void, void (T::*MF)()>
is no better.
Is there some way to specialize on 'void' here ?
Not exactly with the primary class template, that
you provided above. The point is that any function
type R()(A1) with typenames R and A1 will never
match with something that actually has the pattern
R()() which is essentially the same as R()(void) in
C++, see [dcl.fct]/2:
"[..] The parameter list (void) is equivalent to the
empty parameter list. Except for this special case,
void shall not be a parameter type [..]"
Therefore, without variadic template parameters, it
is impossible to express an empty parameter set given
any type argument, e.g. C++0x would allow you to write
a template like this
template<class R, class ... Types> R f(Types ... args);
*including* the case where args corresponds to the empty
parameter set.
Now being in C++03, what about the following approach,
which has some further advantages, because it can be
specialized for even more cases:
template <typename T, T MF>
class D; // Declared, not defined
template<typename A, void (A::*MF)()>
class D<void(A::*)(), MF>
{
};
template<typename A, typename B, void (A::*MF)(B)>
class D<void(A::*)(B), MF>
{
};
struct W {
void foo();
void bar(double);
};
D<void(W::*)(), &W::foo> wf;
D<void(W::*)(double), &W::bar> wb;
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! ]