Re: Explicitly specializing std::min() on VC++ 2005 Express Edition
"Victor Bazarov" <v.Abazarov@comAcast.net> schrieb im Newsbeitrag
news:f1apjp$vqi$1@news.datemas.de...
Add 'const' to the type of the object referred to by arguments:
template<> inline const char* const& min<>
( const char* const& a, const char* const& b) ...
If I do that, then the code compiles, but the specialization is not called
when I pass C-style strings:
#include <algorithm> // Defines std::min().
#include <cstring> // Defines std::strcmp().
#include <iostream> // Defines std::cout and std::endl.
namespace std
{
template <> inline const char * const& min<>
( const char * const& a, const char * const& b )
{
return std::strcmp( a, b ) < 0 ? a : b;
}
}
int main()
{
// Non-const chars.
char* p1 = "b";
char* p2 = "a";
// Calls primary template.
std::cout << std::min( p1, p2 ) << std::endl;
return 0;
}
It gets called, however, if I pass pointers to const chars:
// Chars are const now.
const char* p1 = "b";
const char* p2 = "a";
// Calls specialization.
std::cout << std::min( p1, p2 ) << std::endl;
--
Matthias Hofmann
Anvil-Soft, CEO
http://www.anvil-soft.com - The Creators of Toilet Tycoon
http://www.anvil-soft.de - Die Macher des Klomanagers