On Oct 13, 12:37 pm, Kai-Uwe Bux <jkherci...@gmx.net> wrote:
Fred Zwarts wrote:
Kai-Uwe Bux wrote:
[...]
No, but you could do:
template < typename T, bool IsSigned = is_signed<T>::value >
struct ...
and partially specialize that for <T,true> and <T,false>.
[...]
I tried something along these lines, but it turned out to my
surprise, that partial specializations are not allowed for template
functions, only for template classes/structs. Of course I could
encapsulate the function in a class and add a wrapper function...
Yup. That is annoying. Maybe the most prominent case is specializing a
generic algorithm to take advantage of random access iterators. You
have to wrap the algorithm as a static member function of a class
template, which you can specialize in turn.
Why don't you do like is done in most of the standard libraries:
overload the function with an additional argument:
template< typename RandomIterator >
void f( RandomIterator begin,
RandomIterator end,
std::random_access_iterator_tag )
{
}
template< typename OtherIterator >
void f( OtherIterator begin,
OtherIterator end,
std::input_iterator_tag )
{
}
template< typename Iterator >
void f( Iterator begin, Iterator end )
{
f( begin, end,
typename std::iterator_traits< Iterator
::iterator_category() );
}
(I'm not sure that it's really that much simpler, but it is what I've
seen in the standard library. Note too that it does take advantage of
the inheritance in the iterator tags, so that a forward iterator will
automatically end up in the second overload, without having to
explicitly provide for it.)
That's cute. I had not thaught of that.