Topic: atoi, atof, and atol and c++ good


Author: James Kanze <james.kanze@gmail.com>
Date: Mon, 2 Nov 2009 11:43:20 CST
Raw View
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

--
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@netlab.cs.rpi.edu]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]





Author: Feccarg <ip.quant@gmail.com>
Date: Fri, 30 Oct 2009 13:26:56 CST
Raw View
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-c++@netlab.cs.rpi.edu]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]





Author: "Johannes Schaub (litb)" <schaub-johannes@web.de>
Date: Sat, 31 Oct 2009 00:56:58 CST
Raw View
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.

--
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@netlab.cs.rpi.edu]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]





Author: "Joe Smith" <unknown_kev_cat@hotmail.com>
Date: Sun, 1 Nov 2009 21:08:41 CST
Raw View
"Johannes Schaub (litb)" <schaub-johannes@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.


--
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@netlab.cs.rpi.edu]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]