However, this does mean that the semantics is changed slightly. In the cases where an implicit conversion exists, it may give a different result than if not, in the case of char/wchar_t. Without implict conversion, you get 1 -> '1'. With implicit conversion, you get 1 -> 1 (ASCII 1). As far as I can tell, this only affects conversions between char/wchar_t and other types, though. If this is a problem, please let me know, and I can change it to make an exception for char/wchar_t.
Is this behaviour overridable, for example by adding a specialization transforming 1 => '1' ?
Do you know about any such fast functions or operators (besides the conversion operators mentioned earlier here) for some types? It seems these good old clib functions are pretty fast for conversions. Sorry if they sadly look old-fashioned : atoi(), atol(), strtol(), strtod(), sprintf(), sscanf()... You may laugh at me, but they are, afaik, really the fastest ones :) depending on the platform lib , of course
Maybe, for time types on Unix, asctime(const struct tm *) and strftime() if properly wrapped, all built-ins functions involving complex<> types and their conversion to and from doubles, ints, and so on. PS : Have you considered conversion to and from Roman numbers ;) ;) ? http://www.damtp.cam.ac.uk/user/bp10004/calc_roman.html http://www.maclean-nj.com/romancode.htm ----- Original Message ----- From: Terje Slettebø To: Boost-Users@yahoogroups.com Sent: Thursday, May 23, 2002 2:21 PM Subject: Re: [Boost-Users] boost::lexical_cast string to string problem - New proposition available
From: ravioli@softhome.net
Would it be possible to use an existing conversion operator ? Example : class Output { ... }
class Input { operator Output(void) [ return ... ; } }
Then : lexical_cast<Output>( Input ) ... would use this conversion operator.
It would be possible, yes. That would mean that it uses the fastest and most accurate conversion available, and only resorts to using stringstream when it has to. That seems like a good idea. I've incorporated this change, and updated the version in the Boost files section (http://groups.yahoo.com/group/boost/files/lexical_cast_proposition/). It uses boost::is_convertible for it. Even if that may not work on all compilers, if it doesn't work, it will just resort to using stringstream. Thanks for the suggestion. :) Actually, come to think of it, this also makes some of the special case handling in the previous version obsolete, and that's a good thing. This includes things like converting from char to char, or wchar_t to wchar_t. Therefore, I've removed those special cases, where they are handled by implicit conversion. There's still conversion between char and string, that needs special handling. However, this does mean that the semantics is changed slightly. In the cases where an implicit conversion exists, it may give a different result than if not, in the case of char/wchar_t. Without implict conversion, you get 1 -> '1'. With implicit conversion, you get 1 -> 1 (ASCII 1). As far as I can tell, this only affects conversions between char/wchar_t and other types, though. If this is a problem, please let me know, and I can change it to make an exception for char/wchar_t. Another thing I've been thinking of, is some way to configure the interpreter stream, to be able to change the formatting, such as number base, precision and field width. In fact, I've just now made a version where that is possible, _without_ changing the interface of lexical_cast. I've updated the version in the Boost files version with this. I'll post the details for how to use the stream configuration, in another posting.
This article http://www.cuj.com/experts/1810/alexandr.htm?topic=experts
... describes how it is possible at compile-time to detect whether a class A is convertible to class B, ie if an operator already exists. If so, we would not need in lexical_cast<> to use the serialization operation.
Exactly. In fact, I'm both using the conversion (using boost::is_convertible), and the Int2Type technique descibed in the article (and in "Modern C++ Design"), in order to make it work, to select between implicit conversion, or using stringstream. Without the Int2Type technique, it won't compile. I'm using a different name in the source, though, but the technique is the same: template<bool flag> struct int_to_type { };
What also would be great, is to specialise lexical_cast<> for conversion where is very fast builtin function or operator already exists.
That's also an idea. Again, this means it would use the most efficient way
possible. However, it may not work that well with things like locale, and if
configuration of the interpreter is possible. Maybe have the specialisations
as an option.
Do you know about any such fast functions or operators (besides the
conversion operators mentioned earlier here) for some types?
Thanks for the feedback. :)
Regards,
Terje
----- Original Message -----
From: Terje Slettebø
To: Boost-Users@yahoogroups.com
Sent: Wednesday, May 22, 2002 10:46 PM
Subject: RE: [Boost-Users] boost::lexical_cast string to string problem -
New proposition available
>--- In Boost-Users@y..., Tom Matelich