Boost logo

Boost :

Subject: Re: [boost] [lexical_cast] locale dependent result?
From: Alexander Lamaison (awl03_at_[hidden])
Date: 2011-03-02 08:53:43


On Wed, 2 Mar 2011 04:54:46 -0800 (PST), Artyom wrote:

>> Is the result of lexical_cast<std::string> locale-dependent? One of my
>> contributors has submitted a fix for a problem he says is caused by
>> lexical_cast adding commas to a port number, e.g.: 65535 becomes "65,535".

[..]

> If you want to do casting without locale context (which is desirable)
>
> std::ostringstream ss;
> ss.imbue(std::locale::classic()); // C locale
> ss << 65535;
> return ss.str();

Thanks! So std::locale::classic effectively means 'turn locale awareness
off'?
 
> Unfortunately this problem is very common and causes
> many bugs, especially in C with setlocale and printf.

I can imagine! It's so subtle and unexpected. In fact, I used
lexical_cast precisely because I thought it wouldn't do anything funky with
the conversion.

At least that's how I interpreted the documentation: "For more involved
conversions, such as where precision or formatting need tighter control
than is offered by the default behavior of lexical_cast, the conventional
stringstream approach is recommended."

Alex

-- 
Easy SFTP for Windows Explorer (http://www.swish-sftp.org)

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