Boost logo

Boost :

From: Martin Adrian (adrianm_at_[hidden])
Date: 2004-09-20 13:55:23


Sorry for not keeping the thread. Got some strange errors from when using followup in gmane

>I think that it's not such a big problem. If you want to use locales, then
>your application should start with:
>
> std::locale::global(std::locale(""));
>
>after that, I think lexical_cast will use the default user's locale. Or you
>can replace "" above with something else that your application wants. The
>only problem is that it's not possible to use several locales in one
>application. For program_options, it's not very important.
>
>Why do you need to specify locales for call to 'lexical_cast'?

There are two different cases here. The first one is when you are communicating with the user (e.g. via a GUI). You will then ofcourse use the system locale to format output and parse input.
Since c++ defaults to the "C" locale you need to add the line you mention above to get streams, sorting etc to work properly.

But, it is also common to do things in an application that doesn't follow the system globale like parsing and writing xml or csv files, generating SQL statements etc. (program options might fall into theis category as well)

For the two cases above you need to use different locales.

Since lexical_cast doesn't allow you to specify which locale to use it can only be used for one of the cases above. The current implementation uses the global locale which basicly means that it should only be used for communication with the user. Since there seem to be a strong opinion against specifiying locale for lexical_cast I'm hoping that the documentation is updated to warn about lexical_cast for other things than communicating with the user. I learned it myself the hard way.

When it comes to program options the author has decided to use lexical_cast and the global locale to verify input. It might be a good choice but it means that you can't move configuration files between computers with different locales. Better documentation might be sufficient here as well but there might also be other problems (like space as thousand separators on integers).


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