Re: Explicitly specializing std::min() on VC++ 2005 Express Edition



Matthias Hofmann wrote:
"Tom Widmer [VC++ MVP]" <tom_usenet@xxxxxxxxxxx> schrieb im Newsbeitrag news:ux2I2YajHHA.4516@xxxxxxxxxxxxxxxxxxxxxxx

Ahh, of course that won't work for arrays of char. For your template above, you'd need to do this:

template <class T>
struct min_impl
{
static const T& impl(const T& a, const T& b)
{ return a < b ? a : b; }
};

template <class T> inline
const T& minimum( const T& a, const T& b )
{ return min_impl<T>::impl(a, b); }

Then you can specialize min_impl for char arrays:

template <std::size_t N>
struct min_impl<char[N]>;

This specialization for non-const char[N] seems superflous, as minimum<T>() accepts a constant reference, so min_impl<T>::impl() will never see a non-const char[N].

Good point, though I think it's the other way around - char[N] is needed, but not char const[N].

template <std::size_t N>
struct min_impl<char const[N]>;

What does the definition of this specialization of min_impl look like?

template <std::size_t N>
struct min_impl<char[N]>
{
static char const (&impl(char const (&a)[N], char const (&b)[N]))[N]
{ return std::strcmp(a, b) < 0 ? a : b; }
};

C++ declaration syntax isn't very nice.

There's an additional problem, that the function doesn't allow comparing arrays of different length. That's a bit trickier to handle though!

Tom
.



Relevant Pages


Loading