我建議有一個超載,特別適用於unsigned char
個案,因爲您需要使用中間類型。
bool TryParse(const std::string & value, unsigned char & out)
{
std::istringstream iss(value);
unsigned int i;
iss >> i;
if (iss.fail()) { return false; }
// The less-than-min check is technically redundant because both i and out
// are unsigned, but it makes me feel better having it there. It will become
// necessary for the "signed char" overload, anyway.
if (i > std::numeric_limits<unsigned char>::max() ||
i < std::numeric_limits<unsigned char>::min()) {
throw std::overflow_error();
// Or you could "return false" instead, if that makes more sense.
}
out = static_cast<unsigned char>(i);
return true;
}
對於signed char
,您可以使用幾乎相同的功能。 (只需更換每unsigned
與signed
。)
我不會建議在您的模板中使用中間類型,因爲您需要使用盡可能寬的類型,並且沒有任何一種類型可以工作。例如,unsigned long long int
與signed long long int
不兼容,反之亦然 - 並且這兩種類型都不會與float
或double
兼容。有一個直接使用請求類型的基本模板,並且有問題類型的重載(例如,char
)是正確的方法。
請注意,我已經改變了value
參數是常量字符串的引用,因爲這意味着,調用者不需要字符串複製無緣無故。我會建議改變你的模板功能。
你可以使用'boost :: lexical_cast' - 它不能解決你的具體問題,但它確實使你不會重新發明輪子。 – Chad 2014-09-25 16:15:27
另外,考慮通過引用傳遞'value'。事實上,你迫使調用者無緣無故地複製字符串。 – cdhowie 2014-09-25 16:16:20
爲什麼不爲「char」添加專門化? – Deduplicator 2014-09-25 16:17:56