Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++: partial function specialization for void not allowed - alternative solution?

I think I understand by now why partial function templates are considered confusing and unnecessary, and are thus not allowed by the C++ standard. I would however appreciate some help with re-phrasing the following function w/o partial specialization. FWIW, the function is a member of a non-specialized class:

template <typename IMPL, typename RET>
RET call (functor <IMPL> func, 
          IMPL * impl)
{ 
  return func.call (impl);
}

template <typename IMPL>
void call <IMPL, void_t> (functor <IMPL> func, 
                          IMPL * impl) 
{ 
  func.call (impl);
}

The problem here is that I can't overload on the function's return type. Also, the typename I want to specialize on is not used as function parameter - another reason why overloading does not help. Yes, I could introduce a dummy parameter, to force overloading, but that is ugly, isn't it?

Finally, why the heck isn't 'void' a type in C++? That would make things so much more consistent... But I am probably missing the complete picture...

like image 838
Peer Gynt Avatar asked Dec 09 '22 06:12

Peer Gynt


1 Answers

I believe that, firstly, if you have a function that returns void, then it's perfectly legitimate to return a void expression- such as the call of another function that returns void, and secondly, void is a full type in C++ and you can pass it to templates as much as you like.

like image 160
Puppy Avatar answered Dec 25 '22 23:12

Puppy