public member function
<locale>

std::locale::locale

default (1)
         locale() throw();
copy (2)
         locale (const locale& x) throw();
by-name (3)
explicit locale (const char* std_name);
combining (4)
         locale (const locale& x, const char* std_name, category cats);
template <class Facet>
         locale (const locale& x, const locale& y, category cats);
custom facet (5)
         locale (const locale& x, Facet* f);
default (1)
         locale() noexcept;
copy (2)
         locale (const locale& x) noexcept;
by-name (3)
explicit locale (const char* std_name);
explicit locale (const string& std_name);
combining (4)
         locale (const locale& x, const char* std_name, category cats);
         locale (const locale& x, const string& std_name, category cats);
template <class Facet>
         locale (const locale& x, const locale& y, category cats);
custom facet (5)
         locale (const locale& x, Facet* f);
Locale constructor
Constructs a locale object:

(1) default constructor
Constructs a copy of the current global locale.
The global locale is the locale set by a previous call to locale::global, or locale::classic if locale::global has not been called.
(2) copy constructor
Copies x.
(3) by-name construction
Uses the locale identified by the C-locale name specified by std_name.
(4) combining constructors
Constructs a locale with the same facets as x, except for those specified in cats, which are instead taken from y (or from std_name).
The constructed locale has no name when either x or y has no name.
(5) custom facet constructor
Constructs a copy of x and adds the facet pointed by f to it. If a facet with the same id static member exists in x, f replaces it.
The constructed locale has no name.
Note that this constructor is the only way to add custom facets to standard locale objects.

Parameters

x
Locale copied: the newly constructed object uses the same facets as specified by x (except those specified by cats or f).
std_name
A standard C-locale name.
These are specific of the library implementation, but at least the following two locales are guaranteed to exist on all implementations:
locale namedescription
"C"Minimal "C" locale (the same as locale::classic)
""The environment's default locale
If this argument is a null pointer or does not represent a valid C-locale in the implementation, runtime_error is thrown.
cats
Set of categories that are used from the locale specified as second argument. The remaining categories are taken from the locale specified as first argument.
These shall be any bitwise combination of the values of member type locale::category.
f
Pointer to a facet object.
The resulting locale has this specific facet, with the rest of facet types being those specified by argument x.
The constructed locale object takes over responsibility for deleting this facet object.
If this is a null pointer, the constructed locale is a copy of x.
The constructor uses Facet::id to determine the facet type, replacing its equivalent in x (if it exists).
y
Locale object from which the facets specified in cats are used, with the remaining facets being taken from those in parameter x.

Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
// locale::locale
#include <iostream>       // std::cout
#include <locale>         // std::locale
#include <stdexcept>      // std::runtime_error

int main ()
{
  std::locale loc;     // initialized to locale::classic()

  try {
    loc = std::locale ("en_US.UTF8");
  }
  catch (std::runtime_error) {
    loc = std::locale (loc, "", std::locale::ctype);
  }

  std::cout << "The selected locale is: " << loc.name() << '\n';

  return 0;
}


Possible output:
The selected locale is: en_US.UTF8

Data races

On library implementations that do not maintain a per-thread global locale, the default constructor (1) may introduce data races with concurrent calls to locale::global.

Exception safety

Strong guarantee: no effects in case an exception is thrown.
runtime_error is thrown if std_name is not a valid C-locale name.

See also