Review to_string

This commit is contained in:
Baptiste Wicht 2014-02-11 18:12:00 +01:00
parent e30239b661
commit 327e1244e4

View File

@ -354,10 +354,10 @@ std::vector<std::basic_string<Char>> split(const std::basic_string<Char>& s){
} }
template<typename T> template<typename T>
std::string to_string(T value); std::string to_string(const T& value);
template<> template<>
inline std::string to_string<uint64_t>(uint64_t value){ inline std::string to_string<uint64_t>(const uint64_t& value){
if(value == 0){ if(value == 0){
return "0"; return "0";
} }
@ -366,10 +366,11 @@ inline std::string to_string<uint64_t>(uint64_t value){
char buffer[20]; char buffer[20];
int i = 0; int i = 0;
auto rem = value;
while(value != 0){ while(rem != 0){
buffer[i++] = '0' + value % 10; buffer[i++] = '0' + rem % 10;
value /= 10; rem /= 10;
} }
--i; --i;
@ -382,7 +383,7 @@ inline std::string to_string<uint64_t>(uint64_t value){
} }
template<> template<>
inline std::string to_string<int64_t>(int64_t value){ inline std::string to_string<int64_t>(const int64_t& value){
if(value < 0){ if(value < 0){
std::string s("-"); std::string s("-");
s += to_string(static_cast<uint64_t>(value)); s += to_string(static_cast<uint64_t>(value));
@ -392,6 +393,11 @@ inline std::string to_string<int64_t>(int64_t value){
} }
} }
template<>
inline std::string to_string<unsigned int>(const unsigned int& value){
return to_string(static_cast<size_t>(value));
}
} //end of namespace std } //end of namespace std
#endif #endif