

C++ String to Int - Baustin
http://www.kumobius.com/2013/08/c-string-to-int/

======
krapp
I just finished my first semester in C++, and as someone formerly familiar
with PHP and javascript I was surprised at how much of an issue this is when
you don't have operator overloading and bounds checking and have to manage
memory and types yourself. I was spoiled by just expecting "string"+int to
work.

I ended up taking all user inputs as strings then trying to cast them into
floats with atof() to avoid overflows... which I'll probably learn is a
terrible idea next semester.

------
ksherlock
strtol does fail on overflow [1] -- setting errno to ERANGE and returning
LONG_MIN or LONG_MAX. Since those are also valid return values and it doesn't
update errno on success, it's recommended to set errno to 0 first.

Or, you could c++11 std::stol [2], _et alia_ , which are similar (libc++ is a
wrapper around strtol) but throw an exception on overflow.

1\.
[http://pubs.opengroup.org/onlinepubs/009696699/functions/str...](http://pubs.opengroup.org/onlinepubs/009696699/functions/strtol.html)

2\.
[http://en.cppreference.com/w/cpp/string/basic_string/stol](http://en.cppreference.com/w/cpp/string/basic_string/stol)

