Blood-HaZaRd
Blood-HaZaRd

Reputation: 2124

Templated function accepting char*

I have a templated function and I did not know how to write the specilization for the type *unsigned const char** !?!

I did it for simple types (int, long ...) as below :

template <typename T>
void ConvertTypeToString(const T p_cszValue, std::string& p_szValue)
{
    p_szValue = p_cszValue;     
}

//Template Specialization for int
template <>
void ConvertTypeToString<int>(const int p_iValue, std::string& p_szValue)
{           
    GetFormattedString(p_iValue,p_szValue);
}

//Template Specialization for double
template <>
void ConvertTypeToString<double>(const double p_dValue, std::string& p_szValue)
{               
    GetFormattedString(p_dValue,p_szValue);     
}

And here Where I stuck, I couldn't fugure out what should I write? ther code below dosen't compile.

//for unsigned char* const   
template <>
void ConvertTypeToString<unsigned char*>(const unsigned char* p_ucValue, std::string& p_szValue)
{   
    p_szValue.push_back(p_ucValue);
}

So what is the correct code to write to take in consideration the usigned char* const ?

Than k you

Upvotes: 0

Views: 71

Answers (2)

Erlkoenig
Erlkoenig

Reputation: 2754

It is usually preferred to add an overload instead of a template specialization. This allows you to pass any parameters, including the pointer to const:

void ConvertTypeToString(const unsigned char* const p_ucValue, std::string& p_szValue) { p_szValue.push_back(p_ucValue); }

Upvotes: 1

llllllllll
llllllllll

Reputation: 16404

You placed the const in a wrong place, it should be:

template <>
void ConvertTypeToString<unsigned char*>(unsigned char* const p_ucValue, std::string& p_szValue)
{   
    p_szValue.push_back(p_ucValue);
}

Upvotes: 3

Related Questions