Do I need multiple template specializations if I want to specialize for several kinds of strings?

Posted by romkyns on Stack Overflow See other posts from Stack Overflow or by romkyns
Published on 2010-06-10T11:01:48Z Indexed on 2010/06/10 11:32 UTC
Read the original article Hit count: 253

For example:

template<typename T>
void write(T value)
{
    mystream << value;
}

template<>
void write<const char*>(const char* value)
{
    write_escaped(mystream, value);
}

template<>
void write<char*>(char* value)
{
    write_escaped(mystream, value);
}

template<>
void write<std::string>(std::string value)
{
    write_escaped(mystream.c_str(), value);
}

This looks like I'm doing it wrong, especially the two variants for const and non-const char*. However I checked that if I only specialize for const char * then passing a char * variable will invoke the non-specialized version, when called like this in VC++10:

char something[25];
strcpy(something, "blah");
write(something);

What would be the proper way of doing this?

© Stack Overflow or respective owner

Related posts about c++

Related posts about templates