Boost logo

Boost :

From: Yitzhak Sapir (yitzhaks_at_[hidden])
Date: 2002-03-21 11:43:50


I had recommended before implementing a wlexical_cast for this.
Something along the lines of:

template <typename Target, typename Source, typename Interpreter>
Target basic_lexical_cast(const Source& a_Source, Interpreter
a_Interpreter)
{
    // use a_Interpreter for returning target
}

template <typename Target, typename Source>
Target lexical_cast(const Source& a_Source)
{
    return basic_lexical_cast(a_Source, std::stringstream());
}

template <typename Target, typename Source>
Target wlexical_cast(const Source& a_Source)
{
    return basic_lexical_cast(a_Source, std::wstringstream());
}

> -----Original Message-----
> From: Sven Johannsen [mailto:Sven.Johannsen_at_[hidden]]
> Sent: Thursday, March 21, 2002 1:56 PM
> To: boost_at_[hidden]
> Subject: [boost] lexical_cast makes trouble with wstring
>
>
> the following two lines raise a bad_cast-exception
>
> std::wstring str= L"3.14";
> double pi = boost::lexical_cast<double>(str);
>
> With a look inside the lexical_cast implementation i found a
> std::stringstream.
>
> A std::stringstream is a typedef to a basic_stringstream<char,
> char_traits<char>, allocator<char> >, but in this case i need a
> basic_stringstream<wchar_t, ...>
>
> As a first fix i changed lexical_cast to
>
> template<typename Target, typename Source>
> Target lexical_cast(const Source* arg) //
> the '*' is the change
> {
> //std::stringstream interpreter;
>
> std::basic_stringstream<Source,
> std::char_traits<Source>,std::allocator<Source> > interpreter;
> ...
> }
>
> Now i have to write
>
> double pi = boost::lexical_cast<double>(str.c_str());
>
> This is not the behaviour i want to have.

It also prevents using lexical_cast for converting from double to string

>
> Any Idea?
>
> Sven
>
> _______________________________________________
> Unsubscribe & other changes:
http://lists.boost.org/mailman/listinfo.cgi/boost


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