public static member function
<locale>

std::locale::global

static locale global (const locale& loc);
Set global locale [static]
Sets the global locale to loc.

After the call, the construction of locale objects with its default constructor return a copy of loc.

If loc has a name, the function also sets the C global locale (as if the C library function setlocale was called with LC_ALL), affecting all locale-dependent functions of the C library.
If loc has no name ("*"), the effect on the C global locale depends on the library implementation.

Note that the C++ global locale is always changed to loc by this function, no matter its name. Therefore, if loc has no name, it may cause the C++ global locale (the one used by locale-dependent functions of the C library) to be different from the C++ global locale (the one constructed by default).

Whether a single global locale exists for the entire program, or one for each thread, depends on the library implementation.

Parameters

loc
A locale object to be set as the global locale.

Return value

The previous global locale object.

Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
// locale::global example
#include <iostream>       // std::cout
#include <locale>         // std::locale

int main (void) {
  std::locale foo;     // the "C" locale
  foo.global(std::locale(""));
  std::locale bar;     // the "" locale

  std::cout << "foo and bar are ";
  std::cout << (foo==bar?"the same":"different");
  std::cout << ".\n";

  return 0;
}

Possible output (the "" locale may be the "C" locale, or not):
foo and bar are the same.


Data races

On library implementations that do not maintain a per-thread global locale, the call may introduce data races with concurrent calls to the same function.
Additionally, if the function uses setlocale internally, it may also introduce data races with concurrent calls to the C function setlocale and with any C functions affected by it.

Exception safety

Strong guarantee: if an exception is thrown, there are no effects.

See also