The pointer does not end at the end of the line; end of line is found when the specified value becomes zero. Hence:
while (*source != '\0')
You can, more compactly, write the entire function as follows:
template<class IntType> IntType atoi_unsafe(const char* source) { IntType result = IntType(); char c; while ((c = *source++) != '\0') result = result * 10 + (c - '0'); return result; }
Of course, it does not use the auto keyword. Also note the difference between '\0' and '0' . Brackets in assignment in the loop body are not needed.
Your code only processes unsigned strings - and perhaps confirms that the characters are actually numbers (perhaps throwing an exception if the input is invalid). An “unsafe” appeal certainly applies. Note also that if you instantiate a template for a signed integer type and overflow the value, you invoke undefined behavior. At least with unsigned types, arithmetic is defined, even if probably not what is expected.
source share