the relation between C++ locale and C locale

Z

zade

C has its locale function, and C++ has its locale class. When used, I
don't know their relation. For example , what is the diff for the
codes below:

std::locale::global(std::locale(""));
std::setlocale(LC_ALL,"");

in GNU C++(http://gcc.gnu.org/onlinedocs/libstdc++/manual/
locales.html#manual.localization.locales.locale), it says:

From Josuttis, p. 697-698, which says, that "there is only *one*
relation (of the C++ locale mechanism) to the C locale mechanism: the
global C locale is modified if a named C++ locale object is set as the
global locale" (emphasis Paolo), that is:

std::locale::global(std::locale(""));

affects the C functions as if the following call was made:

std::setlocale(LC_ALL, "");

On the other hand, there is *no* vice versa, that is, calling
setlocale has *no* whatsoever on the C++ locale mechanism, in
particular on the working of locale(""), which constructs the locale
object from the environment of the running program, that is, in
practice, the set of LC_ALL, LANG, etc. variable of the shell.


But in my experience, it is not the same as in windows system.
So what their relation?
 
J

James Kanze

C has its locale function, and C++ has its locale class. When
used, I don't know their relation. For example , what is the
diff for the codes below:

in GNU C++(http://gcc.gnu.org/onlinedocs/libstdc++/manual/
locales.html#manual.localization.locales.locale), it says:
From Josuttis, p. 697-698, which says, that "there is only
*one* relation (of the C++ locale mechanism) to the C locale
mechanism: the global C locale is modified if a named C++
locale object is set as the global locale" (emphasis Paolo),
that is:

affects the C functions as if the following call was made:
std::setlocale(LC_ALL, "");
On the other hand, there is *no* vice versa, that is, calling
setlocale has *no* whatsoever on the C++ locale mechanism, in
particular on the working of locale(""), which constructs the
locale object from the environment of the running program,
that is, in practice, the set of LC_ALL, LANG, etc. variable
of the shell.
But in my experience, it is not the same as in windows system.
So what their relation?

Vague. In fact:

-- std::local::global is guaranteed to call setlocale if the
locale is named. If the locale is not named, the behavior
is implementation defined.

-- Strictly speaking, setlocale is not allowed to influence
std::locale, since "No library function other than
locale::global() shall affect the value returned by
locale()", and setlocale is a library function. In
practice, I'm not sure that this was intended.


Practically speaking, implementations which use an existing C
library for setlocale, without modifying it, cannot have
setlocale modify the global locale, and probably can't do an
effective setlocale if the locale is not names. Other
implementations can do the right thing, however, and keep the
two locales in sync; this is what I would expect from a quality
implementation in cases where the implementors of the C++
library have some control over the C library.
 

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

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top