Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

atoi, atof, and atol and c++ good

106 views
Skip to first unread message

Feccarg

unread,
Oct 30, 2009, 3:26:56 PM10/30/09
to
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?

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 ]

Johannes Schaub (litb)

unread,
Oct 31, 2009, 2:56:58 AM10/31/09
to
Feccarg wrote:

> 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.

Joe Smith

unread,
Nov 1, 2009, 10:08:41 PM11/1/09
to

"Johannes Schaub (litb)" <schaub-...@web.de> wrote in message
news:hcgcv6$2v1$03$1...@news.t-online.com...

> Feccarg wrote:
>
>> 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.

James Kanze

unread,
Nov 2, 2009, 12:43:20 PM11/2/09
to
On Oct 30, 7:26 pm, Feccarg <ip.qu...@gmail.com> wrote:
> 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?

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

0 new messages