Boost logo

Boost :

From: Terje Slettebø (tslettebo_at_[hidden])
Date: 2002-05-22 16:59:28


Hi all.

There's a thread at the Boost User's list now, about some problems with
lexical_cast. From searching the Boost list archive, I also see that various
propositions have been brought up earlier, too.

I've made a version of lexical_cast, that fixes the problem pointed out by
the original poster in that thread (quoted below, here), and I've made it
available in the Boost files section
(http://groups.yahoo.com/group/boost/files/lexical_cast_proposition/). I've
also put a unit test for it, there. Feedback is welcome.

To give background for the proposition, I quote part of the thread below,
here, including my reply at the end, for those who are not subscribed to the
Boost User's list. I'm reposting this here, as it may be relevant here, as
well.

--- Start quotes ---

>--- In Boost-Users_at_y..., "Jason Fischl" <yg-boost-users_at_m...> wrote:

I'm seeing a problem with lexical_cast. The following code when trying to
convert from string to
string fails when passed an empty string. The library code that is doing
this lexical_cast does
not and can not know that the input and output types are the same.

I believe that this code should not throw.

#include <iostream>
#include <boost/lexical_cast.hpp>

main()
{
std::string input("");
try
{
boost::lexical_cast<std::string>(input);
}
catch (boost::bad_lexical_cast& e)
{
std::cout << "Failed" << std::endl;
}
}

========================================================

>--- In Boost-Users_at_y..., Tom Matelich <tmatelich_at_z...> wrote:

I and many others have requested fixes for this for a very long time.
Basically, it involves creating class templates to do the actual conversion.
It's not the most complicated system, just nobody has done it yet. If you
search the boost list archives, you'll see a lot of mail about this. There
is no quick solution to your problem though.

========================================================

--- In Boost-Users_at_y..., Terje Slettebø <terje.s_at_c...> wrote:

>--- In Boost-Users_at_y..., Tom Matelich <tmatelich_at_z...> wrote:
> I and many others have requested fixes for this for a very long time.
> Basically, it involves creating class templates to do the actual
conversion.
> It's not the most complicated system, just nobody has done it yet. If you
> search the boost list archives, you'll see a lot of mail about this.
There
> is no quick solution to your problem though.

I've searched the archive, now, and see that there's been quite a few
propositions for changes, but apparently none of them have ended up in the
lexical_cast implementation.

There's also a couple of propositions, here
(http://groups.yahoo.com/group/boost/files/lexical_cast_propositions/).

Some of the proposed changes are:

- wlexical_cast - A version that can use wide characters
- basic_lexical_cast - A version that takes the interpreter (stringstream)
as a parameter, to support things like changing locale
- Being able to set number base (dec, oct or hex). This would be implicitly
possible using basic_lexical_cast
- Being able to set precision for numerical conversions. This may also be
done with basic_lexical_cast
- Checking for change of sign, when using unsigned numbers. This is
addressed using an integer adapter, in the above lexical_cast_propositions

One problem with doing these changes, after having talked with Kevlin Henney
a while ago (without knowing it had been already proposed, I proposed an
extra parameter, for setting the number base). is that he prefers to not
have extra parameters in the interface, as it then will no longer look like
a cast, and it may be less generic, depending on the way it's done. For
example, a base-setting argument wouldn't make much sense if you converted
from floating point to string.

In any case, I have actually just now made a version of lexical_cast, that
fixes the problem pointed out by OP, here, and I've made it available in the
Boost files section
(http://groups.yahoo.com/group/boost/files/lexical_cast_proposition/). I've
also put a unit test for it, there. Feedback is welcome.

It doesn't change the interface in any way, so it should be ok.

The new version supports conversion between string and char, including char
containing whitespace, and when the string is empty, or containing only
whitespace.

If the source or destination (or both) is basic_string or char/wchar_t, it
will set the interpreter to use the char type used, so it implicitely
supports wide characters. Wide character support requires partial
specialisation if one of the operands is not either std::string/std::wstring
or char/wchar_t.

In the case where the source is basic_string, it also uses pass by const
reference, for efficiency.

It's tested on Intel C++ 6.0, MSVC 6.0 and BCC 5.6.

--- End quotes ---

This way, types are treated more uniformly, which may make it easier to use
lexical_cast in generic code.

Regards,

Terje

P.S. In order to work on BCC, it relies on the new
BOOST_BCB_PARTIAL_SPECIALIZATION_BUG config macro. Apparently, this doesn't
yet exist in the latest Boost version. I guess it may be first updated, when
a new build is made. If that's the case, does anybody when that may be?


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