Click on the banner to return to the class reference home page.

locale


Localization Container

Summary

Localization class containing a polymorphic set of facets.

Data Type and Member Function Indexes
(exclusive of constructors and destructors)

Synopsis

#include<locale> 
class locale;

Description

locale provides a localization interface and a set of indexed facets, each of which covers one particular localization issue. The default locale object is constructed on the "C" locale. Locales can also be constructed on named locales.

A calling program can determine whether a particular facet is contained in a locale by using the has_facet function, and the program can obtain a reference to that facet with the use_facet function. These are not member functions, but instead take a locale object as an argument.

locale has several important characteristics.

First, successive calls to member functions will always return the same result. This allows a calling program to safely cache the results of a call.

Any standard facet not implemented by a locale will be obtained from the global locale the first time that use_facet is called (for that facet).

Only a locale constructed from a name (i.e., "POSIX"), from parts of two named locales, or from a stream, has a name. All other locales are unnamed. Only named locales may be compared for equality. An unnamed locale is equal only to itself.

Interface

class locale {
public:
  // types:
  class facet;
  class id;
  typedef int category;
  static const category   none, collate, ctype, monetary, 
                          numeric, time, messages,
                          all = collate | ctype | monetary | 
                                numeric | time  | messages;
  // construct/copy/destroy:
  locale() throw()
  locale(const locale&) throw()
  explicit locale(const char*);
  locale(const locale&, const char*, category);
  template <class Facet> locale(const locale&, Facet*);
  template <class Facet> locale(const locale&,
                                const locale&);
  locale(const locale&, const locale&, category);
  ~locale() throw();  // non-virtual
  const locale& operator=(const locale&) throw();
  // locale operations:
  basic_string<char>                  name() const;
  bool operator==(const locale&) const;
  bool operator!=(const locale&) const;
  template <class charT,Traits>
    bool operator()(const basic_string<charT,Traits>&,
                    const basic_string<charT,Traits>&) const;
  // global locale objects:
  static       locale  global(const locale&);
  static const locale& classic();
};

class locale::facet {
protected:
  explicit facet(size_t refs = 0);
  virtual ~facet();
private:
  facet(const facet&);          // not defined
  void operator=(const facet&); // not defined
};

class locale::id {
public:
  id();
private:
  void operator=(const id&); // not defined
  id(const id&);             // not defined
};

Types

category
facet
id

Constructors and Destructors

locale()
  throw()
locale(const locale& other)
  throw()
explicit locale(const char* std_name);
locale(const locale& other, const char* std_name,
       category cat);
template <class Facet> 
locale(const locale& other, Facet* f);
template <class Facet> 
locale(const locale& other, const locale& one);





locale(const locale& other, const locale& one, category cat);
~locale(); 

Public Member Operators

const locale& 
operator=(const locale& other) throw();
bool 
operator==(const locale& other) const;
bool 
operator!=(const locale& other) const;
template <class charT,Traits>
bool 
operator()(const basic_string<charT,Traits>& s1,
           const basic_string<charT,Traits>& s2) const;

Public Member Functions

basic_string<char> 
name() const;

Static Public Member Functions

static locale  
global(const locale& loc);




static const locale& 
classic();




Example

//
// locale.cpp
//

 #include <string>
 #include <vector>
 #include <iostream>
 #include "codecvte.h"

 int main ()
 {
  using namespace std;

  locale loc;  // Default locale

  // Construct new locale using default locale plus
  // user defined codecvt facet
  // This facet converts from ISO Latin 
  // Alphabet No. 1 (ISO 8859-1) to 
  // U.S. ASCII code page 437
  // This facet replaces the default for
  // codecvt<char,char,mbstate_t>
  locale my_loc(loc,new ex_codecvt);

  // imbue modified locale onto cout
  locale old = cout.imbue(my_loc);
  cout << "A \x93 jolly time was had by all" << endl;

  cout.imbue(old);
  cout << "A jolly time was had by all" << endl;

  // Create a vector of strings 
  vector<string,allocator<void> > v;
  v.insert(v.begin(),"antelope");
  v.insert(v.begin(),"bison");
  v.insert(v.begin(),"elk");

  copy(v.begin(),v.end(),
       ostream_iterator<string,char,
                char_traits<char> >(cout," "));
  cout << endl;

  // Sort the strings using the locale as a comparitor
  sort(v.begin(),v.end(),loc);

  copy(v.begin(),v.end(),
       ostream_iterator<string,char,
               char_traits<char> >(cout," "));

  cout << endl;
  return 0;
}

See Also

facets, has_facet, use_facet, specific facet reference sections


©Copyright 1996, Rogue Wave Software, Inc.