www.digitalmars.com Home | Search | C & C++ | D | DMDScript | News Groups | index | prev | next
Archives

D Programming
D
D.gnu
digitalmars.D
digitalmars.D.bugs
digitalmars.D.dtl
digitalmars.D.dwt
digitalmars.D.announce
digitalmars.D.learn
digitalmars.D.debugger

C/C++ Programming
c++
c++.announce
c++.atl
c++.beta
c++.chat
c++.command-line
c++.dos
c++.dos.16-bits
c++.dos.32-bits
c++.idde
c++.mfc
c++.rtl
c++.stl
c++.stl.hp
c++.stl.port
c++.stl.sgi
c++.stlsoft
c++.windows
c++.windows.16-bits
c++.windows.32-bits
c++.wxwindows

digitalmars.empire
digitalmars.DMDScript

c++ - Bug: template explicit specialization with strcmp

↑ ↓ ← KTC <me here.com> writes:
#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
Apr 19 2005
↑ ↓ → "Walter" <newshound digitalmars.com> writes:
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*.
Apr 22 2005