April 19, 2005 Bug: template explicit specialization with strcmp | ||||
---|---|---|---|---|
| ||||
#include <iostream> #include <cstring> // generic template definition template <typename T> T max( T t1, T t2 ) { return (t1 > t2 ? t1 : t2); } // const char* explicit specialization: // overrides instantiation from the generic template definition typedef const char *PCC; template<> PCC max( PCC s1, PCC s2 ) { return ( std::strcmp( s1, s2 ) > 0 ? s1 : s2 ); } int main() { // call to instantiation: int max< int >( int, int ); int i = max( 10, 5 ); // call to explicit specialization: // const char* max< const char* >( const char*, const char* ); const char *p = max( "hello", "world" ); std::cout << "i: " << i << " p: " << p << std::endl; return 0; } ******************** Should print: i: 10 p: world Instead print: i: 10 p: hello KTC -- Experience is a good school but the fees are high. - Heinrich Heine |
April 22, 2005 Re: template explicit specialization with strcmp | ||||
---|---|---|---|---|
| ||||
Posted in reply to KTC | The problem isn't with the templates, they work fine. The problem is the compiler assigns the type 'char*' to strings rather than 'const char*'. I'm not willing to fix this at the moment because it'll break an unknown amount of code. To workaround, create a specialization that works off of char*. |
Copyright © 1999-2021 by the D Language Foundation