The code below generates a warning with -Wsign-conversion
. It generates the warning at the line T digit = a % base
.
I want to extract the signed-ness of T
and then cast base
to that signed-ness to squash the warning.
I'm trying to avoid specializations because that just duplicates code (the only thing that's going to change is the signed-ness of base
). I'm also trying to avoid casting base
to a T
in case its a non-POD type like a Integer
(which is optimized for the reduction with longs
).
How do I extract the signed-ness of T
?
Related, the code base is effectively C++98 and C++03, so it does not have some features (like discussed in Partial template specialization based on “signed-ness” of integer type?).
template <class T>
std::string IntToString(T a, unsigned int base = 10)
{
if (a == 0)
return "0";
bool negate = false;
if (a < 0)
{
negate = true;
a = 0-a; // VC .NET does not like -a
}
std::string result;
while (a > 0)
{
T digit = a % base;
result = char((digit < 10 ? '0' : ('a' - 10)) + digit) + result;
a /= base;
}
if (negate)
result = "-" + result;
return result;
}