Re: Explicitly specializing std::min() on VC++ 2005 Express Edition
"Tom Widmer [VC++ MVP]" <tom_usenet@hotmail.com> schrieb im Newsbeitrag
news:ux2I2YajHHA.4516@TK2MSFTNGP03.phx.gbl...
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].
template <std::size_t N>
struct min_impl<char const[N]>;
What does the definition of this specialization of min_impl look like? On
VC++ 2005 Express Edition, I get a compile time error with the following
code:
#include <cstring>
#include <iostream>
template <class T> struct minimum_impl
{
static const T& minimum(
const T& a, const T& b )
{
return a < b ? a : b;
}
};
template <std::size_t N> struct minimum_impl<char[N]>
{
// Compile time error for the following line!
static const char(&)[N] minimum(
const char a(&)[N], const char b(&)[N] )
{
return std::strcmp( a, b ) < 0 ? a : b;
}
};
template <class T> inline
const T& minimum( const T& a, const T& b )
{
return minimum_impl<T>::minimum( a, b );
}
int main()
{
char a[] = "a";
char b[] = "b";
std::cout << minimum( a, b ) << std::endl;
return 0;
}
--
Matthias Hofmann
Anvil-Soft, CEO
http://www.anvil-soft.com - The Creators of Toilet Tycoon
http://www.anvil-soft.de - Die Macher des Klomanagers