Boost logo

Boost :

From: Jeff Garland (jeff_at_[hidden])
Date: 2006-05-03 09:03:23


Oleg Abrosimov wrote:
> Jeff Garland:
>
> Ok I see your point. It would be interesting though to make a polling on
> c.l.c++.m on this question just to see the whole picture.

Be my guest, but I think you have it right below.

> anyway, usage of C-style cast is not required.
> for simple initialization one can do:
> double d = from_string("10.2");

> and if one has a real need to use it in a complex expression:
> double d = 2 + static_cast<double>(from_string("10.2"));
>
> or, better:
>
> double d = from_string("10.2");
> d += 2;

Honestly, this is more like it anyway -- trivial way to avoid all the
casts -- not doubts at all what the code does. No casting fungus to
start growing ;-)

> and if one wants to be very modern ;-) but doesn't concern about
> symmetry in from_string/string_from usage :
> double d = 2 + from_string<double>("10.2");

This is fine too. Perfect symmetry isn't really needed.

> it could be supported in parallel with non-templated version. (see code
> below)
>
> > Well I'm afraid that I see one serious problem with this idea that
> lexical cast avoids -- ambiguity. The following code will not compile on
> g++ 3.3.2 -- totally bogus implementations, but just to give you the idea:
>
>...snip jeff's code...
> no, try the following code:
>
> #include <iostream>
> #include <sstream>
>
> struct fs_helper
> {
> fs_helper(std::string const& s) :
> m_str(s) {}
> template <typename T>
> operator T() {
> std::stringstream ss(m_str);
> T t;
> ss >> t;
> return t;
> }
>
> private :
> std::string m_str;
> };
>
> inline fs_helper from_string(std::string const& str)
> {
> return fs_helper(str);
> }
>
> template <typename T>
> inline T from_string(std::string const& str)
> {
> return static_cast<T>(fs_helper(str));
> }
>
> int main()
> {
> double d0 = from_string("10.2");
> double d = 2 + (double)from_string("10.2");
> double d2 = 4 + from_string<double>("10.2");
> std::cout << d0 << std::endl;
> std::cout << d << std::endl;
> std::cout << d2 << std::endl;
> return 0;
> }
>
> it perfectly works on VC7.1 at least.
> and compiles by

Sure, this is fine -- as I said, the only way I could see it working was
as a template function -- which wasn't entirely obvious from your
earlier posts.

But in the end, I fail to see how it really differs significantly from
lexical_cast. They are both template function interfaces. Yes, I like
the names better than lexical_cast. It is less generic, but by the time
you are done you will need exceptions and templatization on string types
-- all elements of lexical cast. In the base implementation you still
have all the stringstream performance issues that people complain about
with lexical_cast. I assume the idea is to specialize the common cases
so they are faster than the stringstream implementation? Of course this
could be done with lexical_cast -- it's been discussed many times and is
not precluded by the Henney/Dawes proposal. In fact, I expect it's
assumed that vendors would do better than Boost has so far.

Let me wrap up by saying I'm not trying to discourage you -- just trying
to make sure a realistic expectation / comparison is done upfront.

Jeff


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk