Topic: No from_string()?
Author: "george.ryan@gmail.com" <george.ryan@gmail.com>
Date: Wed, 25 Aug 2010 11:43:55 CST Raw View
Hi,
I noticed that =A721.5 allows a to_string() function for numerical
conversions, but not a from_string(). It seems natural to have that
functionality be symmetric; why would that not have been added?
Thanks!
George
--
[ 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<std-c%2B%2B@netlab.cs.rpi.edu>
]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html ]
Author: Pete Becker <pete@versatilecoding.com>
Date: Wed, 25 Aug 2010 16:17:57 CST Raw View
On 2010-08-25 07:43:55 -0400, george.ryan@gmail.com said:
>
> I noticed that =A721.5 allows a to_string() function for numerical
> conversions, but not a from_string(). It seems natural to have that
> functionality be symmetric; why would that not have been added?
>
If you're pulling things out of a string you pretty much need a stream
object to handle the parsing. Not so when you're building a string:
std::string res = "the value is: " + to_string(3.14159);
--
Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of "The
Standard C++ Library Extensions: a Tutorial and Reference
(www.petebecker.com/tr1book)
[ 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: =?ISO-8859-1?Q?Daniel_Kr=FCgler?= <daniel.kruegler@googlemail.com>
Date: Wed, 25 Aug 2010 16:18:30 CST Raw View
On 25 Aug., 19:43, "george.r...@gmail.com" <george.r...@gmail.com>
wrote:
> I noticed that =A721.5 allows a to_string() function for numerical
> conversions, but not a from_string(). It seems natural to have that
> functionality be symmetric; why would that not have been added?
The symmetry is only superficial. The to_string
function accept rvalues or lvalues of T and return
a std::string, but it would be impossible to realize
the exact symmetry, i.e.
- keep the same name
- accept rvalues and lvalues of std::string
- return T
because functions cannot be overloaded on the return
type. The current approach is to relax the first requirement
and to keep the two other ones.
Alternatives approaches are:
- Accept only mutable T as the destination of the conversion
To use such a function, you always need a variable of
T first and this is a show-stopper for the actual reason
to provide the utility functions.
- Make from_string a template, e.g.
template<T>
T from_string(const std::string& str, size_t *idx = 0, int base = 10);
While this would support all three requirements mentioned
in the list, it opens a can of worms in regard to the question:
Where do we stop?
a) Why is the source not also a free parameter?
b) Should the library allow from_string to be a customization
point?
This quickly ends up in something like boost::lexical_cast.
While such a tool is surely useful, this was not the motivation
for providing the tiny little conversion helpers.
HTH & Greetings from Bremen,
Daniel Kr gler
--
[ 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: Gennaro Prota <gennaro.prota@yahoo.com>
Date: Wed, 25 Aug 2010 23:57:40 CST Raw View
On 26/08/2010 0.17, Pete Becker wrote:
> On 2010-08-25 07:43:55 -0400, george.ryan@gmail.com said:
>
>>
>> I noticed that =A721.5 allows a to_string() function for numerical
>> conversions, but not a from_string(). It seems natural to have that
>> functionality be symmetric; why would that not have been added?
>>
>
> If you're pulling things out of a string you pretty much need a stream
> object to handle the parsing. Not so when you're building a string:
>
> std::string res = "the value is: " + to_string(3.14159);
So what are all the stoxyz functions doing? :-)
I understand that it came out really too long, otherwise it
would have been nice to have your comments on this:
<http://groups.google.com/group/comp.std.c++/browse_thread/thread/8a5b9412b1526ab9/54f4a60825b15267?#54f4a60825b15267>
But if you feel like skimming through it and reply only on
selected parts, that would be nice too ;-)
--
Gennaro Prota | I'm available for your projects.
Breeze (preview): <https://sourceforge.net/projects/breeze/>
[ 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 ]