If so, why in the first place, c++ standard doesn't provide atoi c++
equivalent?
The question could be expanded to other c libraries, like, to cmath
library. As the usage of mathematical functions is very frequent, I
cannot think any reason why standard c++ shouldn't provide its own c++
equivalent mathematical functions.
Ilkka
--
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std...@netlab.cs.rpi.edu]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html ]
> I've have been told numerous times that it is not good practice to mix
> c and c++, and I see why: Standard c functions don't have exception
> handling or, for example, atoi doesn't have overflow checking. If I
> still happen to need a string to integer conversion while programming c
> ++, I feel that I'm in a dead-end. I shouldn't use atoi as it is c,
> but on the other hand c++ standard doesn't (to the best of my
> knowledge) provide atoi c++ equivalent. So if I want to avoid using C,
> am I forced to write my own atoi/atof/atol equivalent function?
>
> If so, why in the first place, c++ standard doesn't provide atoi c++
> equivalent?
>
This has been fixed and the C++0x draft includes functions
std::stoi/stol/stoul/stoll/stoull that do this and throw appropriately if
the conversion fails.
In the mean time, the recomended way is to wrap the string in a stringstream
and extract the integer (or other number) from there, right? Admittedly a
pain, and not as nice as functions like you just mentioned, but possible.
> If so, why in the first place, c++ standard doesn't provide
> atoi c++ equivalent?
Because the function really shouldn't be used in C, either, for
the reasons you state. The usual function in C would be stdtol
or stdtoul; this can also be used in C++, if you have a C style
array of char (and there are more than a few cases where it is
appropriate); otherwise, use the string to initialize an
istringstream and read from that.
> The question could be expanded to other c libraries, like, to
> cmath library. As the usage of mathematical functions is very
> frequent, I cannot think any reason why standard c++ shouldn't
> provide its own c++ equivalent mathematical functions.
How would they differ from those in C?
--
James Kanze