|
Boost : |
From: Early Ehlinger (earlye_at_[hidden])
Date: 2002-12-28 20:22:41
"Terje Slettebø" <tslettebo_at_[hidden]> wrote:
> e.g. the following one gives the same problem, not solved with only
> checking for if source type equals target type:
>
> std::string str=boost::lexical_cast<std::string>(' '); // Space character,
> throws exception
Indeed. I took a look at the proposed fix and it seems to fix both
problems. Thank you for taking the time to implement the new solution.
As a devil's advocate, consider, e.g., Borland's AnsiString class (part of
their VCL and CLX libraries, basically a class that represents the intrinsic
String type in Pascal/Delphi):
AnsiString foo = "Hello there";
std::string str = boost::lexical_cast< std::string >( foo ); // exception
for same reason!
This leads me to wonder if there isn't a more generic solution to the
problem. For example, it might be useful to have a mechanism to determine
if source is a string type and if so, use an implementation that looks
something like this:
std::stringstream temp;
temp << source;
return TargetType( temp.str().c_str() );
When I say "string type", I mean any type that represents the concept of a
string, not just those that are implemented via std::basic_string. Not sure
what exactly the best way to do this is; perhaps a lexical_cast_traits<>
template that the user must provide to help out? So right after I include
the definition of AnsiString, I could do something like this:
template<> struct lexical_cast_traits< AnsiString > : public
lexical_cast_string_type { };
And now lexical_cast would "know" that AnsiString is a string type.
Similar things could be done to support CString from MFC as well as the
zillions of other string implementations out there.
-- -- Early Ehlinger CEO, ResPower Inc - Toll-Free : 866-737-7697 -- - RenderFarm - Lightwave , 3dSMax , Bryce , Maya , AfterEffects - --- www.respower.com -- 200+ GHz Starting At USD$0.50/GHz*Hour -- ----------------- SuperComputing For the Masses! ---------------- "I'm so busy programming my computer to let me be lazy, I never get any rest!" - John Kaster
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk