I have a class, Delegate
, declared like this:
template<typename T> class Delegate;
template<typename R, typename... Args>
class Delegate<R(Args...)>
{ /*...*/ };
It can be instantiated for a function returning a ReturnType
and taking no arguments as a Delegate<ReturnType()>
. I've run into an issue that requires me to specialize the class' ()
operator for this case, but haven't been able to figure out how to coerce the compiler doing so without a compiler error.
I have the following function:
template <typename R, typename... Args>
R Delegate<R(Args...)>::operator()(Args... args)
{ /*...*/ }
Adding the following specialization, I get an an error saying invalid use of incomplete type 'class Delegate<R()>'
:
template <typename R>
R Delegate<R()>::operator()()
{ /*...*/ }
but I can't simply replace Args...
with void
either, as far as I've been able to tell... What is the proper procedure here, and (if this question applies and you are feeling extra helpful) why?
Your attempt with using
R Delegate<R()>::operator()()
to specialize even more the member function of a partial specialization of a class template fails due to ยง14.5.5.3 [temp.class.spec.mfunc]:In other words:
is actually a specialization of
operator()
of your primary template:and since it's an incomplete type, you end up with the error. The possible workarounds are:
Option #1
Specialize the entire class and reimplement all the members of that class:
DEMO 1
Option #2
Use a one more delegate class that is specialized:
DEMO 2
Option #3
Use some ugly SFINAE:
DEMO 3
Option #4
Inherit from a specialized class template, possibly utilizing the CRTP idiom:
DEMO 4