locale question

C

Cagdas Ozgenc

Greetings.

I am confused about C++ standard library locale stuff. It seems as if the
implementations of locales are not part of the library, but only some
guideline classes are there.

What is the standard conformant way of formatting and parsing locale
specific date, time, strings with various character encoding, etc.

Thanks
 
M

Mark Kerns

I am confused about C++ standard library locale stuff. It seems as if the
implementations of locales are not part of the library, but only some
guideline classes are there.

What is the standard conformant way of formatting and parsing locale
specific date, time, strings with various character encoding, etc.

Thanks

Typically you'll use a locale through a stream but you can also use them
directly. When working with a stream, you simply invoke the stream's "imbue"
member, passing your "locale" object which is encapsulated in the stream.
You can read up on the various locale constructors but the "C" locale (AKA
locale::classic) is used by default (actually, the "locale::global" object
is used but it originates from "locale::classic" unless you change it). You
can pass other implementation-defined strings to a locale's constructor
however, typically based on RFC 1766 which in turn is based on ISO 639 and
3166 - try passing "en-US" (English US ) or "fr-CA" (French Canadian) for
instance or consult your local implementation for details). Subsequently,
whenever you invoke the << or >> operators on your stream, the stream will
handle the date, time, etc. according to the facets stored in the
encapsulated locale (a locale is really just a collection of facet objects
such as "num_put", "time_put", "numpunct" etc.). You can also store your own
customized facets in an existing locale however or provide overrides for the
various (facet) member functions as required. In any case, everything boils
down to invoking the "use_facet()" function template, passing a given
facet's class name as the template arg, the locale you want as the
function's arg, and then invoking a particular member of that facet's class
(that is, a reference to the requested facet object is returned by this
function and you just invoke the member you're interested in). "use_facet()"
is rather ugly to call as you'll soon see (the entire locale/facet design is
ugly IMO) but you should consult this function for details. Also see
"has_facet()" to ensure a facet is even supported by a given locale.
Everything should become clear once you understand "use_facet()" which
forces you to address the issues you're asking about (also note that the <<
and >> stream operators defer to this function in case that's not clear by
now).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,535
Members
45,007
Latest member
obedient dusk

Latest Threads

Top