Skip to content
Find file
Fetching contributors…
Cannot retrieve contributors at this time
5991 lines (5217 sloc) 175 KB
\rSec0[localization]{Localization library}
\rSec1[localization.general]{General}
\pnum
This Clause describes components that \Cpp programs may use to
encapsulate (and therefore be more portable when confronting)
cultural differences.
The locale facility includes internationalization
support for character classification and string collation, numeric,
monetary, and date/time formatting and parsing, and message retrieval.
\pnum
The following subclauses describe components for
locales themselves, the standard facets, and facilities
from the ISO C library, as summarized in Table~\ref{tab:localization.lib.summary}.
\begin{libsumtab}{Localization library summary}{tab:localization.lib.summary}
\ref{locales} & Locales & \tcode{<locale>} \\
\ref{locale.categories} & Standard \tcode{locale} Categories & \\ \rowsep
\ref{locale.stdcvt} & Standard code conversion facets & \tcode{<codecvt>} \\ \rowsep
\ref{c.locales} & C library locales & \tcode{<clocale>} \\
\end{libsumtab}
\rSec1[locale.syn]{Header \tcode{<locale>} synopsis}
\indexlibrary{\idxhdr{locale}}%
\begin{codeblock}
namespace std {
// \ref{locale}, locale:
class locale;
template <class Facet> const Facet& use_facet(const locale&);
template <class Facet> bool has_facet(const locale&) noexcept;
// \ref{locale.convenience}, convenience interfaces:
template <class charT> bool isspace (charT c, const locale& loc);
template <class charT> bool isprint (charT c, const locale& loc);
template <class charT> bool iscntrl (charT c, const locale& loc);
template <class charT> bool isupper (charT c, const locale& loc);
template <class charT> bool islower (charT c, const locale& loc);
template <class charT> bool isalpha (charT c, const locale& loc);
template <class charT> bool isdigit (charT c, const locale& loc);
template <class charT> bool ispunct (charT c, const locale& loc);
template <class charT> bool isxdigit(charT c, const locale& loc);
template <class charT> bool isalnum (charT c, const locale& loc);
template <class charT> bool isgraph (charT c, const locale& loc);
template <class charT> charT toupper(charT c, const locale& loc);
template <class charT> charT tolower(charT c, const locale& loc);
template <class Codecvt, class Elem = wchar_t,
class Wide_alloc = std::allocator<Elem>,
class Byte_alloc = std::allocator<char> > class wstring_convert;
template <class Codecvt, class Elem = wchar_t,
class Tr = char_traits<Elem@\shr{}@ class wbuffer_convert;
// \ref{category.ctype}, ctype:
class ctype_base;
template <class charT> class ctype;
template <> class ctype<char>; // specialization
template <class charT> class ctype_byname;
class codecvt_base;
template <class internT, class externT, class stateT> class codecvt;
template <class internT, class externT, class stateT> class codecvt_byname;
// \ref{category.numeric}, numeric:
template <class charT, class InputIterator = istreambuf_iterator<charT> > class num_get;
template <class charT, class OutputIterator = ostreambuf_iterator<charT> > class num_put;
template <class charT> class numpunct;
template <class charT> class numpunct_byname;
// \ref{category.collate}, collation:
template <class charT> class collate;
template <class charT> class collate_byname;
// \ref{category.time}, date and time:
class time_base;
template <class charT, class InputIterator = istreambuf_iterator<charT> >
class time_get;
template <class charT, class InputIterator = istreambuf_iterator<charT> >
class time_get_byname;
template <class charT, class OutputIterator = ostreambuf_iterator<charT> >
class time_put;
template <class charT, class OutputIterator = ostreambuf_iterator<charT> >
class time_put_byname;
// \ref{category.monetary}, money:
class money_base;
template <class charT, class InputIterator = istreambuf_iterator<charT> > class money_get;
template <class charT, class OutputIterator = ostreambuf_iterator<charT> > class money_put;
template <class charT, bool Intl = false> class moneypunct;
template <class charT, bool Intl = false> class moneypunct_byname;
// \ref{category.messages}, message retrieval:
class messages_base;
template <class charT> class messages;
template <class charT> class messages_byname;
}
\end{codeblock}
\pnum
The header
\tcode{<locale>}
defines classes and declares functions that encapsulate and manipulate
the information peculiar to a locale.\footnote{In this subclause, the type name
\tcode{struct tm}
is an incomplete type that is defined in
\indexlibrary{\idxhdr{ctime}}%
\tcode{<ctime>}.}
\rSec1[locales]{Locales}
\rSec2[locale]{Class \tcode{locale}}
\indexlibrary{\idxhdr{locale}}%
\begin{codeblock}
namespace std {
class locale {
public:
// types:
class facet;
class id;
typedef int category;
static const category // values assigned here are for exposition only
none = 0,
collate = 0x010, ctype = 0x020,
monetary = 0x040, numeric = 0x080,
time = 0x100, messages = 0x200,
all = collate | ctype | monetary | numeric | time | messages;
// construct/copy/destroy:
locale() noexcept;
locale(const locale& other) noexcept;
explicit locale(const char* std_name);
explicit locale(const string& std_name);
locale(const locale& other, const char* std_name, category);
locale(const locale& other, const string& std_name, category);
template <class Facet> locale(const locale& other, Facet* f);
locale(const locale& other, const locale& one, category);
~locale(); // not virtual
const locale& operator=(const locale& other) noexcept;
template <class Facet> locale combine(const locale& other) const;
// locale operations:
basic_string<char> name() const;
bool operator==(const locale& other) const;
bool operator!=(const locale& other) const;
template <class charT, class traits, class Allocator>
bool operator()(const basic_string<charT,traits,Allocator>& s1,
const basic_string<charT,traits,Allocator>& s2) const;
// global locale objects:
static locale global(const locale&);
static const locale& classic();
};
}
\end{codeblock}
\pnum
Class
\tcode{locale}
implements a type-safe polymorphic set of facets, indexed by facet
\textit{type}.
In other words, a facet has a dual role: in one
sense, it's just a class interface; at the same time, it's an index
into a locale's set of facets.
\pnum
Access to the facets of a
\tcode{locale}
is via two function templates,
\tcode{use_facet<>}
and
\tcode{has_facet<>}.
\pnum
\enterexample
An iostream
\tcode{operator\shl}
might be implemented as:\footnote{Note that in the call to
\tcode{put}
the stream is implicitly converted to an
\tcode{ostreambuf_iterator<charT,traits>}.}
\begin{codeblock}
template <class charT, class traits>
basic_ostream<charT,traits>&
operator<< (basic_ostream<charT,traits>& s, Date d) {
typename basic_ostream<charT,traits>::sentry cerberos(s);
if (cerberos) {
ios_base::iostate err = ios_base::iostate::goodbit;
tm tmbuf; d.extract(tmbuf);
use_facet< time_put<charT,ostreambuf_iterator<charT,traits> > >(
s.getloc()).put(s, s, s.fill(), err, &tmbuf, 'x');
s.setstate(err); // might throw
}
return s;
}
\end{codeblock}
\exitexample
\pnum
In the call to
\tcode{use_facet<Facet>(loc)},
the type argument chooses a facet, making available all members
of the named type.
If
\tcode{Facet}
is not present in a
locale,
it throws the standard exception
\tcode{bad_cast}.
A \Cpp program can check if a locale implements a particular
facet with the
function template
\tcode{has_facet<Facet>()}.
User-defined facets may be installed in a locale, and used identically as
may standard facets~(\ref{facets.examples}).
\pnum
\enternote
All locale semantics are accessed via
\tcode{use_facet<>}
and
\tcode{has_facet<>},
except that:
\begin{itemize}
\item
A member operator template
\tcode{operator()(const basic_string<C, T, A>\&, const basic_string<\brk{}C, T, A>\&)}
is provided so that a locale may be used as a predicate argument to
the standard collections, to collate strings.
\item
Convenient global interfaces are provided for traditional
\tcode{ctype}
functions such as
\tcode{isdigit()}
and
\tcode{isspace()},
so that given a locale
object \tcode{loc} a \Cpp program can call
\tcode{isspace(c,loc)}.
(This eases upgrading existing extractors~(\ref{istream.formatted}).)
\exitnote
\end{itemize}
\pnum
Once a facet reference is obtained from a locale object by calling
\tcode{use_facet<>},
that reference remains usable, and the results from member functions
of it may be cached and re-used, as long as some locale object refers
to that facet.
\pnum
In successive calls to a locale facet member function on a facet object
installed in the same locale, the returned result shall be identical.
\pnum
A
\tcode{locale}
constructed from a name string (such as \tcode{"POSIX"}), or from parts of
two named locales, has a name; all others do not.
Named locales may be compared for equality; an unnamed locale is equal
only to (copies of) itself.
For an unnamed locale,
\tcode{locale::name()}
returns the string
\tcode{"*"}.
\pnum
Whether there is one global locale object for the entire program or one global locale
object per thread is \impldef{whether locale object is global or per-thread}.
Implementations should provide one global locale object per
thread. If there is a single global locale object for the entire program,
implementations are not required to avoid data races on it~(\ref{res.on.data.races}).
\rSec3[locale.types]{\tcode{locale} types}
\rSec4[locale.category]{Type \tcode{locale::category}}
\indexlibrary{\idxcode{locale}!\idxcode{category}}%
\indexlibrary{\idxcode{category}!\idxcode{locale}}%
\begin{itemdecl}
typedef int category;
\end{itemdecl}
\pnum
\textit{Valid}
\tcode{category}
values include the
\tcode{locale}
member bitmask elements
\tcode{collate},
\tcode{ctype},
\tcode{monetary},
\tcode{numeric},
\tcode{time},
and
\tcode{messages},
each of which represents a single locale category.
In addition,
\tcode{locale}
member bitmask constant
\tcode{none}
is defined as zero and represents no category. And
\tcode{locale}
member bitmask constant
\tcode{all}
is defined such that the expression
\begin{codeblock}
(collate | ctype | monetary | numeric | time | messages | all) == all
\end{codeblock}
is
\tcode{true},
and represents the union of all categories.
Further, the expression
\tcode{(X | Y)},
where
\tcode{X}
and
\tcode{Y}
each represent a single category, represents the union of the two categories.
\pnum
\tcode{locale}
member functions expecting a
\tcode{category}
argument require one of the
\tcode{category}
values defined above, or the union of two or more such values.
Such a
\tcode{category}
value identifies a set of locale categories.
Each locale category,
in turn, identifies a set of locale facets, including at least those
shown in Table~\ref{tab:localization.category.facets}.
\begin{floattable}{Locale category facets}{tab:localization.category.facets}
{ml}
\topline
\lhdr{Category} & \rhdr{Includes facets} \\ \capsep
collate & \tcode{collate<char>}, \tcode{collate<wchar_t>} \\ \rowsep
ctype & \tcode{ctype<char>}, \tcode{ctype<wchar_t>} \\
& \tcode{codecvt<char,char,mbstate_t>} \\
& \tcode{codecvt<char16_t,char,mbstate_t>} \\
& \tcode{codecvt<char32_t,char,mbstate_t>} \\
& \tcode{codecvt<wchar_t,char,mbstate_t>} \\ \rowsep
monetary & \tcode{moneypunct<char>}, \tcode{moneypunct<wchar_t>} \\
& \tcode{moneypunct<char,true>}, \tcode{moneypunct<wchar_t,true>} \\
& \tcode{money_get<char>}, \tcode{money_get<wchar_t>} \\
& \tcode{money_put<char>}, \tcode{money_put<wchar_t>} \\ \rowsep
numeric & \tcode{numpunct<char>}, \tcode{numpunct<wchar_t>} \\
& \tcode{num_get<char>}, \tcode{num_get<wchar_t>} \\
& \tcode{num_put<char>}, \tcode{num_put<wchar_t>} \\ \rowsep
time & \tcode{time_get<char>}, \tcode{time_get<wchar_t>} \\
& \tcode{time_put<char>}, \tcode{time_put<wchar_t>} \\ \rowsep
messages & \tcode{messages<char>}, \tcode{messages<wchar_t>} \\
\end{floattable}
\pnum
For any locale \tcode{loc}
either constructed, or returned by
\tcode{locale::classic()},
and any facet \tcode{Facet}
shown in Table~\ref{tab:localization.category.facets},
\tcode{has_facet<Facet>(loc)}
is true.
Each
\tcode{locale}
member function which takes a
\tcode{locale::category}
argument operates on the corresponding set of facets.
\pnum
An implementation is required to provide those specializations for
facet templates identified as members of a category, and for those
shown in Table~\ref{tab:localization.required.specializations}.
\begin{floattable}{Required specializations}{tab:localization.required.specializations}
{ml}
\topline
\lhdr{Category} & \rhdr{Includes facets} \\ \capsep
collate & \tcode{collate_byname<char>}, \tcode{collate_byname<wchar_t>} \\ \rowsep
ctype & \tcode{ctype_byname<char>}, \tcode{ctype_byname<wchar_t>} \\
& \tcode{codecvt_byname<char,char,mbstate_t>} \\
& \tcode{codecvt_byname<char16_t,char,mbstate_t>} \\
& \tcode{codecvt_byname<char32_t,char,mbstate_t>} \\
& \tcode{codecvt_byname<wchar_t,char,mbstate_t>} \\ \rowsep
monetary & \tcode{moneypunct_byname<char,International>} \\
& \tcode{moneypunct_byname<wchar_t,International>} \\
& \tcode{money_get<C,InputIterator>} \\
& \tcode{money_put<C,OutputIterator>} \\ \rowsep
numeric & \tcode{numpunct_byname<char>}, \tcode{numpunct_byname<wchar_t>} \\
& \tcode{num_get<C,InputIterator>}, \tcode{num_put<C,OutputIterator>} \\ \rowsep
time & \tcode{time_get<char,InputIterator>} \\
& \tcode{time_get_byname<char,InputIterator>} \\
& \tcode{time_get<wchar_t,InputIterator>} \\
& \tcode{time_get_byname<wchar_t,InputIterator>} \\
& \tcode{time_put<char,OutputIterator>} \\
& \tcode{time_put_byname<char,OutputIterator>} \\
& \tcode{time_put<wchar_t,OutputIterator>} \\
& \tcode{time_put_byname<wchar_t,OutputIterator>} \\ \rowsep
messages & \tcode{messages_byname<char>}, \tcode{messages_byname<wchar_t>} \\
\end{floattable}
\pnum
The provided implementation of members of facets
\tcode{num_get<charT>}
and
\tcode{num_put<charT>}
calls
\tcode{use_fac\-et\,<F>\,(l)}
only for facet
\tcode{F}
of types
\tcode{numpunct<charT>}
and
\tcode{ctype<charT>},
and for locale
\tcode{l}
the value obtained
by calling member
\tcode{getloc()}
on the
\tcode{ios_base\&}
argument to these functions.
\pnum
In declarations of facets, a template formal parameter with name
\tcode{InputIterator}
or
\tcode{OutputIterator}
indicates the set of
all possible specializations on parameters that satisfy the
requirements of an Input Iterator or an Output Iterator, respectively~(\ref{iterator.requirements}).
A template formal parameter with name
\tcode{C}
represents the set
of types containing \tcode{char}, \tcode{wchar_t}, and any other implementation-defined
character types that satisfy
the requirements for a character on which any of the iostream
components can be instantiated.
A template formal parameter with name
\tcode{International}
represents the set of all possible specializations on a bool parameter.
\rSec4[locale.facet]{Class \tcode{locale::facet}}
\indexlibrary{\idxcode{locale}!\idxcode{facet}}%
\indexlibrary{\idxcode{facet}!\idxcode{locale}}%
\begin{codeblock}
namespace std {
class locale::facet {
protected:
explicit facet(size_t refs = 0);
virtual ~facet();
facet(const facet&) = delete;
void operator=(const facet&) = delete;
};
}
\end{codeblock}
\pnum
Template parameters in this Clause which are required to be facets are those named
\tcode{Facet}
in declarations.
A program that passes a type that is
\textit{not}
a facet, or a type that refers to a volatile-qualified facet, as an
(explicit or deduced) template parameter to a locale
function expecting a facet, is ill-formed. A const-qualified facet is a
valid template argument to any locale function that expects a Facet template
parameter.
\pnum
The \tcode{refs}
argument to the constructor is used for lifetime management.
\begin{itemize}
\item
For
\tcode{refs == 0},
the implementation performs
\tcode{delete static_cast<locale::facet*>(f)}
(where
\tcode{f}
is a point\-er to the facet) when the last
\tcode{locale}
object containing the facet is destroyed;
for
\tcode{refs == 1},
the implementation never destroys the facet.
\end{itemize}
\pnum
Constructors of all
facets defined in this Clause take such an argument and pass it
along to their
\tcode{facet}
base class constructor.
All one-argument constructors defined
in this Clause are
\term{explicit},
preventing their participation in automatic conversions.
\pnum
For some standard facets a standard
``$\ldots$\tcode{_byname}''
class, derived from it, implements the virtual function semantics
equivalent to that facet of the locale constructed by
\tcode{locale(const char*)}
with the same name.
Each such facet provides a constructor that takes a
\tcode{const char*}
argument, which names the locale, and a \tcode{refs}
argument, which is passed to the base class constructor.
Each such facet also provides a constructor that takes a
\tcode{string} argument \tcode{str} and a \tcode{refs}
argument, which has the same effect as calling the first constructor with the
two arguments \tcode{str.c_str()} and \tcode{refs}.
If there is no
``$\ldots$\tcode{_byname}''
version of a facet, the base class implements named locale
semantics itself by reference to other facets.
\rSec4[locale.id]{Class \tcode{locale::id}}
\indexlibrary{\idxcode{locale}!\idxcode{id}}%
\indexlibrary{\idxcode{id}!\idxcode{locale}}%
\begin{codeblock}
namespace std {
class locale::id {
public:
id();
void operator=(const id&) = delete;
id(const id&) = delete;
};
}
\end{codeblock}
\pnum
The class locale::id provides identification of a locale facet
interface, used as an index for lookup
and to encapsulate initialization.
\pnum
\enternote
Because facets are used by iostreams, potentially while static constructors are
running, their initialization cannot depend on programmed static
initialization.
One initialization strategy is for
\tcode{locale}
to initialize each facet's
\tcode{id}
member the first time an instance of the facet is installed into a locale.
This depends only on static storage being zero before constructors run~(\ref{basic.start.init}).
\exitnote
\rSec3[locale.cons]{\tcode{locale} constructors and destructor}
\indexlibrary{\idxcode{locale}!constructor}%
\begin{itemdecl}
locale() noexcept;
\end{itemdecl}
\begin{itemdescr}
\pnum
Default constructor: a snapshot of the current global locale.
\pnum
\effects
Constructs a copy of the argument last passed to
\tcode{locale::global(locale\&)},
if it has been called; else, the resulting facets have virtual
function semantics identical to those of
\tcode{locale::classic()}.
\enternote
This constructor is commonly used as the default value for arguments
of functions that take a
\tcode{const locale\&}
argument.
\exitnote
\end{itemdescr}
\indexlibrary{\idxcode{locale}!constructor}%
\begin{itemdecl}
locale(const locale& other) noexcept;
\end{itemdecl}
\begin{itemdescr}
\pnum
\effects
Constructs a locale which is a copy of \tcode{other}.
\end{itemdescr}
\begin{itemdescr}
\pnum
\effects
Creates a copy of \tcode{other}, replacing the current value.
\pnum
\returns
\tcode{*this}
\end{itemdescr}
\indexlibrary{\idxcode{locale}!constructor}%
\begin{itemdecl}
explicit locale(const char* std_name);
\end{itemdecl}
\begin{itemdescr}
\pnum
\effects
Constructs a locale using standard C locale names, e.g., \tcode{"POSIX"}.
The resulting locale implements semantics defined to be associated
with that name.
\pnum
\throws
\tcode{runtime_error}
if the argument is not valid, or is null.
\pnum
\notes
The set of valid string argument values is \tcode{"C"}, \tcode{""},
and any \impldef{locale names} values.
\end{itemdescr}
\indexlibrary{\idxcode{locale}!constructor}%
\begin{itemdecl}
explicit locale(const string& std_name);
\end{itemdecl}
\begin{itemdescr}
\pnum
\effects The same as \tcode{locale(std_name.c_str())}.
\end{itemdescr}
\indexlibrary{\idxcode{locale}!constructor}%
\begin{itemdecl}
locale(const locale& other, const char* std_name, category);
\end{itemdecl}
\begin{itemdescr}
\pnum
\effects
Constructs a locale as a copy of
\tcode{other}
except for the facets identified by the
\tcode{category}
argument, which instead implement the same semantics as
\tcode{locale(std_name)}.
\pnum
\throws
\tcode{runtime_error}
if the argument is not valid, or is null.
\pnum
\notes
The locale has a name if and only if
\tcode{other}
has a name.
\end{itemdescr}
\indexlibrary{\idxcode{locale}!constructor}%
\begin{itemdecl}
locale(const locale& other, const string& std_name, category cat);
\end{itemdecl}
\begin{itemdescr}
\pnum
\effects The same as \tcode{locale(other, std_name.c_str(), cat)}.
\end{itemdescr}
\indexlibrary{\idxcode{locale}!constructor}%
\begin{itemdecl}
template <class Facet> locale(const locale& other, Facet* f);
\end{itemdecl}
\begin{itemdescr}
\pnum
\effects
Constructs a locale incorporating all facets from the first
argument except that of type
\tcode{Facet},
and installs the second argument as the remaining facet.
If \tcode{f}
is null, the resulting object is a copy of \tcode{other}.
\pnum
\notes
The resulting locale has no name.
\end{itemdescr}
\indexlibrary{\idxcode{locale}!constructor}%
\begin{itemdecl}
locale(const locale& other, const locale& one, category cats);
\end{itemdecl}
\begin{itemdescr}
\pnum
\effects
Constructs a locale incorporating all facets from the first argument
except those that implement
\tcode{cats},
which are instead incorporated from the second argument.
\pnum
\notes
The resulting locale has a name if and only if the first two arguments
have names.
\end{itemdescr}
\indexlibrary{\idxcode{operator=}!\idxcode{locale}}%
\indexlibrary{\idxcode{locale}!\idxcode{operator=}}%
\begin{itemdecl}
const locale& operator=(const locale& other) noexcept;
\end{itemdecl}
\indexlibrary{\idxcode{locale}!destructor}%
\begin{itemdecl}
~locale();
\end{itemdecl}
\begin{itemdescr}
\pnum
A non-virtual destructor that throws no exceptions.
\end{itemdescr}
\rSec3[locale.members]{\tcode{locale} members}
\indexlibrary{\idxcode{locale}!\idxcode{combine}}%
\indexlibrary{\idxcode{combine}!\idxcode{locale}}%
\begin{itemdecl}
template <class Facet> locale combine(const locale& other) const;
\end{itemdecl}
\begin{itemdescr}
\pnum
\effects
Constructs a locale incorporating
all facets from
\tcode{*this}
except for that one facet of
\tcode{other}
that is identified by
\tcode{Facet}.
\pnum
\returns
The newly created locale.
\pnum
\throws
\tcode{runtime_error}
if
\tcode{has_facet<Facet>(other)}
is false.
\pnum
\notes
The resulting locale has no name.
\end{itemdescr}
\indexlibrary{\idxcode{locale}!\idxcode{name}}%
\indexlibrary{\idxcode{name}!\idxcode{locale}}%
\begin{itemdecl}
basic_string<char> name() const;
\end{itemdecl}
\begin{itemdescr}
\pnum
\returns
The name of
\tcode{*this},
if it has one; otherwise, the string \tcode{"*"}.
If
\tcode{*this}
has a name, then
\tcode{locale(name().\brk{}c\textunderscore{}str())}
is equivalent to
\tcode{*this}.
Details of the contents of the resulting string are otherwise implementation-defined.%
\indextext{implementation-defined}
\end{itemdescr}
\rSec3[locale.operators]{\tcode{locale} operators}
\indexlibrary{\idxcode{locale}!\idxcode{operators==}}%
\indexlibrary{\idxcode{operators==}!\idxcode{locale}}%
\begin{itemdecl}
bool operator==(const locale& other) const;
\end{itemdecl}
\begin{itemdescr}
\pnum
\returns
\tcode{true}
if both arguments are the same locale, or one is a copy of the
other, or each has a name and the names are identical;
\tcode{false}
otherwise.
\end{itemdescr}
\indexlibrary{\idxcode{locale}!\idxcode{operator"!=}}%
\indexlibrary{\idxcode{operator"!=}!\idxcode{locale}}%
\begin{itemdecl}
bool operator!=(const locale& other) const;
\end{itemdecl}
\begin{itemdescr}
\pnum
\returns
The result of the expression:
\tcode{!(*this == other)}.
\end{itemdescr}
\indexlibrary{\idxcode{locale}!\idxcode{operator()}}%
\indexlibrary{\idxcode{operator()}!\idxcode{locale}}%
\begin{itemdecl}
template <class charT, class traits, class Allocator>
bool operator()(const basic_string<charT,traits,Allocator>& s1,
const basic_string<charT,traits,Allocator>& s2) const;
\end{itemdecl}
\begin{itemdescr}
\pnum
\effects
Compares two strings according to the
\tcode{collate<charT>}
facet.
\pnum
\notes
This member operator template (and therefore
\tcode{locale}
itself) satisfies requirements for a comparator predicate template argument
(Clause~\ref{algorithms}) applied to strings.
\pnum
\returns
The result of the following expression:
\begin{codeblock}
use_facet< collate<charT> >(*this).compare
(s1.data(), s1.data()+s1.size(), s2.data(), s2.data()+s2.size()) < 0;
\end{codeblock}
\pnum
\enterexample
A vector of strings
\tcode{v}
can be collated according to collation rules in locale
\tcode{loc}
simply by~(\ref{alg.sort}, \ref{vector}):
\begin{codeblock}
std::sort(v.begin(), v.end(), loc);
\end{codeblock}
\exitexample
\end{itemdescr}
\rSec3[locale.statics]{\tcode{locale} static members}
\indexlibrary{\idxcode{locale}!\idxcode{global}}%
\indexlibrary{\idxcode{global}!\idxcode{locale}}%
\begin{itemdecl}
static locale global(const locale& loc);
\end{itemdecl}
\begin{itemdescr}
\pnum
Sets the global locale to its argument.
\pnum
\effects
Causes future calls to the constructor
\tcode{locale()}
to return a copy of the argument.
If the argument has a name, does
\begin{codeblock}
std::setlocale(LC_ALL, loc.name().c_str());
\end{codeblock}
otherwise, the effect on the C locale, if any, is \impldef{effect on C locale of calling
\tcode{locale::global}}.
No library function other than
\tcode{locale\colcol{}global()}
shall affect the value returned by
\tcode{locale()}.
\enternote See~\ref{c.locales} for data race considerations when
\tcode{setlocale} is invoked. \exitnote
\pnum
\returns
The previous value of
\tcode{locale()}.
\end{itemdescr}
\indexlibrary{\idxcode{locale}!\idxcode{classic}}%
\indexlibrary{\idxcode{classic}!\idxcode{locale}}%
\begin{itemdecl}
static const locale& classic();
\end{itemdecl}
\begin{itemdescr}
\pnum
The \tcode{"C"} locale.
\pnum
\returns
A locale that implements the classic \tcode{"C"} locale semantics, equivalent
to the value \tcode{locale("C")}.
\pnum
\notes
This locale, its facets, and their member functions, do not change
with time.
\end{itemdescr}
\rSec2[locale.global.templates]{\tcode{locale} globals}
\indexlibrary{\idxcode{locale}!\idxcode{use_facet}}%
\indexlibrary{\idxcode{use_facet}!\idxcode{locale}}%
\begin{itemdecl}
template <class Facet> const Facet& use_facet(const locale& loc);
\end{itemdecl}
\begin{itemdescr}
\pnum
\requires
\tcode{Facet}
is a facet class whose definition contains the public static member
\tcode{id}
as defined in~\ref{locale.facet}.
\pnum
\returns
A reference to the corresponding facet of \tcode{loc}, if present.
\pnum
\throws
\tcode{bad_cast}
if
\tcode{has_facet<Facet>(loc)}
is
\tcode{false}.
\pnum
\notes
The reference returned remains valid at least as long as any copy of
\tcode{loc} exists.
\end{itemdescr}
\indexlibrary{\idxcode{locale}!\idxcode{has_facet}}%
\indexlibrary{\idxcode{has_facet}!\idxcode{locale}}%
\begin{itemdecl}
template <class Facet> bool has_facet(const locale& loc) noexcept;
\end{itemdecl}
\begin{itemdescr}
\pnum
\returns
True if the facet requested is present in \tcode{loc}; otherwise false.
\end{itemdescr}
\rSec2[locale.convenience]{Convenience interfaces}
\rSec3[classification]{Character classification}
\indexlibrary{\idxcode{isspace}}%
\indexlibrary{\idxcode{isprint}}%
\indexlibrary{\idxcode{iscntrl}}%
\indexlibrary{\idxcode{isupper}}%
\indexlibrary{\idxcode{islower}}%
\indexlibrary{\idxcode{isalpha}}%
\indexlibrary{\idxcode{isdigit}}%
\indexlibrary{\idxcode{ispunct}}%
\indexlibrary{\idxcode{isxdigit}}%
\indexlibrary{\idxcode{isalnum}}%
\indexlibrary{\idxcode{isgraph}}%
\indexlibrary{\idxcode{isblank}}%
\begin{itemdecl}
template <class charT> bool isspace (charT c, const locale& loc);
template <class charT> bool isprint (charT c, const locale& loc);
template <class charT> bool iscntrl (charT c, const locale& loc);
template <class charT> bool isupper (charT c, const locale& loc);
template <class charT> bool islower (charT c, const locale& loc);
template <class charT> bool isalpha (charT c, const locale& loc);
template <class charT> bool isdigit (charT c, const locale& loc);
template <class charT> bool ispunct (charT c, const locale& loc);
template <class charT> bool isxdigit(charT c, const locale& loc);
template <class charT> bool isalnum (charT c, const locale& loc);
template <class charT> bool isgraph (charT c, const locale& loc);
template <class charT> bool isblank (charT c, const locale& loc);
\end{itemdecl}
\pnum
Each of these functions
\tcode{is\textbf{\textit{F}}}
returns the result of the expression:
\begin{codeblock}
use_facet< ctype<charT> >(loc).is(ctype_base::@\textbf{\textit{F}}@, c)
\end{codeblock}
where \textbf{\textit{F}} is the
\tcode{ctype_base::mask}
value corresponding to that function~(\ref{category.ctype}).\footnote{When
used in a loop, it is faster to cache the
\tcode{ctype<>}
facet and use it directly, or use the vector form of
\tcode{ctype<>::is}.}
\rSec3[conversions]{Conversions}
\rSec4[conversions.character]{Character conversions}
\indexlibrary{\idxcode{toupper}}%
\begin{itemdecl}
template <class charT> charT toupper(charT c, const locale& loc);
\end{itemdecl}
\begin{itemdescr}
\pnum
\returns
\tcode{use_facet<ctype<charT> >(loc).toupper(c)}.
\end{itemdescr}
\indexlibrary{\idxcode{tolower}}%
\begin{itemdecl}
template <class charT> charT tolower(charT c, const locale& loc);
\end{itemdecl}
\begin{itemdescr}
\pnum
\returns
\tcode{use_facet<ctype<charT> >(loc).tolower(c)}.
\end{itemdescr}
\rSec4[conversions.string]{\tcode{string} conversions}
\pnum
Class template \tcode{wstring_convert} performs conversions between a wide
string and a byte string. It lets you specify a code conversion facet
(like class template \tcode{codecvt}) to perform the conversions, without
affecting any streams or locales. \enterexample Say, for example, you have a code
conversion facet called \tcode{codecvt_utf8} that you want to use to output to
\tcode{cout} a UTF-8 multibyte sequence corresponding to a wide string, but you
don't want to alter the locale for \tcode{cout}. You can write something like:
\begin{codeblock}
wstring_convert<codecvt_utf8<wchar_t>> myconv;
std::string mbstring = myconv.to_bytes(L"Hello\n");
std::cout << mbstring;
\end{codeblock}
\exitexample
\pnum
\synopsis{Class template \tcode{wstring_convert} synopsis}
\indexlibrary{\idxcode{wstring_convert}}%
\begin{codeblock}
namespace std {
template<class Codecvt, class Elem = wchar_t,
class Wide_alloc = std::allocator<Elem>,
class Byte_alloc = std::allocator<char> > class wstring_convert {
public:
typedef std::basic_string<char, char_traits<char>, Byte_alloc> byte_string;
typedef std::basic_string<Elem, char_traits<Elem>, Wide_alloc> wide_string;
typedef typename Codecvt::state_type state_type;
typedef typename wide_string::traits_type::int_type int_type;
wstring_convert(Codecvt *pcvt = new Codecvt);
wstring_convert(Codecvt *pcvt, state_type state);
wstring_convert(const byte_string& byte_err,
const wide_string& wide_err = wide_string());
~wstring_convert();
wide_string from_bytes(char byte);
wide_string from_bytes(const char *ptr);
wide_string from_bytes(const byte_string& str);
wide_string from_bytes(const char *first, const char *last);
byte_string to_bytes(Elem wchar);
byte_string to_bytes(const Elem *wptr);
byte_string to_bytes(const wide_string& wstr);
byte_string to_bytes(const Elem *first, const Elem *last);
size_t converted() const;
state_type state() const;
private:
byte_string byte_err_string; // \expos
wide_string wide_err_string; // \expos
Codecvt *cvtptr; // \expos
state_type cvtstate; // \expos
size_t cvtcount; // \expos
};
}
\end{codeblock}
\pnum
The class template describes an object that controls conversions between wide
string objects of class \tcode{std::basic_string<Elem, char_traits<Elem>,
Wide_alloc>} and byte string objects of class \tcode{std::\\basic_string<char,
char_traits<char>, Byte_alloc>}. The class template defines the types
\tcode{wide_string} and \tcode{byte_string} as synonyms for these two types.
Conversion between a sequence of \tcode{Elem} values (stored in a wide_string
object) and multibyte sequences (stored in a byte_string object) is performed by
an object of class \tcode{Codecvt<Elem, char, std::mbstate_t>}, which meets the
requirements of the standard code-conversion facet \tcode{std::codecvt<Elem,
char, std::mbstate_t>}.
\pnum
An object of this class template stores:
\begin{itemize}
\item \tcode{byte_err_string} --- a byte string to display on errors
\item \tcode{wide_err_string} --- a wide string to display on errors
\item \tcode{cvtptr} --- a pointer to the allocated conversion object
(which is freed when the \tcode{wstring_convert} object is destroyed)
\item \tcode{cvtstate} --- a conversion state object
\item \tcode{cvtcount} --- a conversion count
\end{itemize}
\indexlibrary{\idxcode{byte_string}!\idxcode{wstring_convert}}%
\indexlibrary{\idxcode{wstring_convert}!\idxcode{byte_string}}%
\begin{itemdecl}
typedef std::basic_string<char> byte_string;
\end{itemdecl}
\begin{itemdescr}
\pnum
The type shall be a synonym for \tcode{std::basic_string<char>}
\end{itemdescr}
\indexlibrary{\idxcode{converted}!\idxcode{wstring_convert}}%
\indexlibrary{\idxcode{wstring_convert}!\idxcode{converted}}%
\begin{itemdecl}
size_t converted() const;
\end{itemdecl}
\begin{itemdescr}
\pnum
\returns \tcode{cvtcount}.
\end{itemdescr}
\indexlibrary{\idxcode{from_bytes}!\idxcode{wstring_convert}}%
\indexlibrary{\idxcode{wstring_convert}!\idxcode{from_bytes}}%
\begin{itemdecl}
wide_string from_bytes(char byte);
wide_string from_bytes(const char *ptr);
wide_string from_bytes(const byte_string& str);
wide_string from_bytes(const char *first, const char *last);
\end{itemdecl}
\begin{itemdescr}
\pnum
\effects
The first member function shall convert the single-element sequence \tcode{byte} to a
wide string. The second member function shall convert the null-terminated
sequence beginning at \tcode{ptr} to a wide string. The third member function
shall convert the sequence stored in \tcode{str} to a wide string. The fourth member
function shall convert the sequence defined by the range \range{first}{last} to a
wide string.
\pnum
In all cases:
\begin{itemize}
\item If the \tcode{cvtstate} object was not constructed with an explicit value, it
shall be set to its default value (the initial conversion state) before the
conversion begins. Otherwise it shall be left unchanged.
\item The number of input elements successfully converted shall be stored in \tcode{cvtcount}.
\end{itemize}
\pnum
\returns
If no conversion error occurs, the member function shall return the converted wide string.
Otherwise, if the object was constructed with a wide-error string, the
member function shall return the wide-error string.
Otherwise, the member function throws an object of class \tcode{std::range_error}.
\end{itemdescr}
\indexlibrary{\idxcode{int_type}!\idxcode{wstring_convert}}%
\indexlibrary{\idxcode{wstring_convert}!\idxcode{int_type}}%
\begin{itemdecl}
typedef typename wide_string::traits_type::int_type int_type;
\end{itemdecl}
\begin{itemdescr}
The type shall be a synonym for \tcode{wide_string::traits_type::int_type}.
\end{itemdescr}
\indexlibrary{\idxcode{state}!\idxcode{wstring_convert}}%
\indexlibrary{\idxcode{wstring_convert}!\idxcode{state}}%
\begin{itemdecl}
state_type state() const;
\end{itemdecl}
\begin{itemdescr}
\pnum
returns \tcode{cvtstate}.
\end{itemdescr}
\indexlibrary{\idxcode{state_type}!\idxcode{wstring_convert}}%
\indexlibrary{\idxcode{wstring_convert}!\idxcode{state_type}}%
\begin{itemdecl}
typedef typename Codecvt::state_type state_type;
\end{itemdecl}
\begin{itemdescr}
\pnum
The type shall be a synonym for \tcode{Codecvt::state_type}.
\end{itemdescr}
\indexlibrary{\idxcode{to_bytes}!\idxcode{wstring_convert}}%
\indexlibrary{\idxcode{wstring_convert}!\idxcode{to_bytes}}%
\begin{itemdecl}
byte_string to_bytes(Elem wchar);
byte_string to_bytes(const Elem *wptr);
byte_string to_bytes(const wide_string& wstr);
byte_string to_bytes(const Elem *first, const Elem *last);
\end{itemdecl}
\begin{itemdescr}
\pnum
\effects
The first member function shall convert the single-element sequence \tcode{wchar} to a byte string.
The second member function shall convert the null-terminated sequence beginning at \tcode{wptr} to
a byte string. The third member function shall convert the sequence stored in \tcode{wstr} to a
byte string. The fourth member function shall convert the sequence defined by the
range \range{first}{last} to a byte string.
\pnum
In all cases:
\begin{itemize}
\item If the \tcode{cvtstate} object was not constructed with an explicit value, it
shall be
set to its default value (the initial conversion state) before the
conversion begins. Otherwise it shall be left unchanged.
\item The number of input elements successfully converted shall be stored
in \tcode{cvtcount}.
\end{itemize}
\pnum
\returns
If no conversion error occurs, the member function shall return the converted byte string.
Otherwise, if the object was constructed with a byte-error string, the
member function shall return the byte-error string.
Otherwise, the member function shall throw an object of class \tcode{std::range_error}.
\end{itemdescr}
\indexlibrary{\idxcode{wide_string}!\idxcode{wstring_convert}}%
\indexlibrary{\idxcode{wstring_convert}!\idxcode{wide_string}}%
\begin{itemdecl}
typedef std::basic_string<Elem> wide_string;
\end{itemdecl}
\begin{itemdescr}
\pnum
The type shall be a synonym for \tcode{std::basic_string<Elem>}.
\end{itemdescr}
\indexlibrary{\idxcode{wstring_convert}!constructor}%
\begin{itemdecl}
wstring_convert(Codecvt *pcvt = new Codecvt);
wstring_convert(Codecvt *pcvt, state_type state);
wstring_convert(const byte_string& byte_err,
const wide_string& wide_err = wide_string());
\end{itemdecl}
\begin{itemdescr}
\pnum
\effects
The first constructor shall store \tcode{pcvt} in \tcode{cvtptr} and
default values in \tcode{cvtstate}, \tcode{byte_err_string}, and
\tcode{wide_err_string}.
The second constructor shall store \tcode{pcvt} in \tcode{cvtptr},
\tcode{state} in \tcode{cvtstate}, and default values in
\tcode{byte_err_string} and \tcode{wide_err_string}; moreover the
stored state shall be retained between calls to \tcode{from_bytes} and
\tcode{to_bytes}.
The third constructor shall store \tcode{new Codecvt} in \tcode{cvtptr},
\tcode{state_type()} in \tcode{cvtstate}, \tcode{byte_err}
in \tcode{byte_err_string}, and \tcode{wide_err} in
\tcode{wide_err_string}.
\end{itemdescr}
\indexlibrary{\idxcode{wstring_convert}!destructor}%
\begin{itemdecl}
~wstring_convert();
\end{itemdecl}
\begin{itemdescr}
\pnum
\effects The destructor shall delete \tcode{cvtptr}.
\end{itemdescr}
\rSec4[conversions.buffer]{Buffer conversions}
\pnum
Class template \tcode{wbuffer_convert} looks like a wide stream buffer, but
performs all its I/O through an underlying byte stream buffer that you
specify when you construct it. Like class template \tcode{wstring_convert}, it
lets you specify a code conversion facet to perform the conversions,
without affecting any streams or locales.
\pnum
\synopsis{Class template \tcode{wbuffer_convert} synopsis}
\indexlibrary{\idxcode{wbuffer_convert}}%
\begin{codeblock}
namespace std {
template<class Codecvt,
class Elem = wchar_t,
class Tr = std::char_traits<Elem> >
class wbuffer_convert
: public std::basic_streambuf<Elem, Tr> {
public:
typedef typename Codecvt::state_type state_type;
wbuffer_convert(std::streambuf *bytebuf = 0,
Codecvt *pcvt = new Codecvt,
state_type state = state_type());
std::streambuf *rdbuf() const;
std::streambuf *rdbuf(std::streambuf *bytebuf);
state_type state() const;
private:
std::streambuf *bufptr; // \expos
Codecvt *cvtptr; // \expos
state_type cvtstate; // \expos
};
}
\end{codeblock}
\pnum
The class template describes a stream buffer that controls the
transmission of elements of type \tcode{Elem}, whose character traits are
described by the class \tcode{Tr}, to and from a byte stream buffer of type
\tcode{std::streambuf}. Conversion between a sequence of \tcode{Elem} values and
multibyte sequences is performed by an object of class
\tcode{Codecvt<Elem, char, std::mbstate_t>}, which shall meet the requirements
of the standard code-conversion facet \tcode{std::codecvt<Elem, char, std::mbstate_t>}.
\pnum
An object of this class template stores:
\begin{itemize}
\item \tcode{bufptr} --- a pointer to its underlying byte stream buffer
\item \tcode{cvtptr} --- a pointer to the allocated conversion object
(which is freed when the \tcode{wbuffer_convert} object is destroyed)
\item \tcode{cvtstate} --- a conversion state object
\end{itemize}
\indexlibrary{\idxcode{state}!\idxcode{wbuffer_convert}}%
\indexlibrary{\idxcode{wbuffer_convert}!\idxcode{state}}%
\begin{itemdecl}
state_type state() const;
\end{itemdecl}
\begin{itemdescr}
\pnum
\returns \tcode{cvtstate}.
\end{itemdescr}
\indexlibrary{\idxcode{rdbuf}!\idxcode{wbuffer_convert}}%
\indexlibrary{\idxcode{wbuffer_convert}!\idxcode{rdbuf}}%
\begin{itemdecl}
std::streambuf *rdbuf() const;
\end{itemdecl}
\begin{itemdescr}
\pnum
\returns \tcode{bufptr}.
\end{itemdescr}
\indexlibrary{\idxcode{rdbuf}!\idxcode{wbuffer_convert}}%
\indexlibrary{\idxcode{wbuffer_convert}!\idxcode{rdbuf}}%
\begin{itemdecl}
std::streambuf *rdbuf(std::streambuf *bytebuf);
\end{itemdecl}
\begin{itemdescr}
\pnum
\effects stores \tcode{bytebuf} in \tcode{bufptr}.
\pnum
\returns The previous value of \tcode{bufptr}.
\end{itemdescr}
\indexlibrary{\idxcode{state_type}!\idxcode{wbuffer_convert}}%
\indexlibrary{\idxcode{wbuffer_convert}!\idxcode{state_type}}%
\begin{itemdecl}
typedef typename Codecvt::state_type state_type;
\end{itemdecl}
\begin{itemdescr}
\pnum
The type shall be a synonym for \tcode{Codecvt::state_type}.
\end{itemdescr}
\indexlibrary{\idxcode{wbuffer_convert}!constructor}%
\begin{itemdecl}
wbuffer_convert(std::streambuf *bytebuf = 0,
Codecvt *pcvt = new Codecvt, state_type state = state_type());
\end{itemdecl}
\begin{itemdescr}
\pnum
\effects The constructor constructs a stream buffer object, initializes
\tcode{bufptr} to \tcode{bytebuf}, initializes \tcode{cvtptr}
to \tcode{pcvt}, and initializes \tcode{cvtstate} to \tcode{state}.
\end{itemdescr}
\indexlibrary{\idxcode{wbuffer_convert}!destructor}%
\begin{itemdecl}
~wbuffer_convert();
\end{itemdecl}
\begin{itemdescr}
\pnum
\effects The destructor shall delete \tcode{cvtptr}.
\end{itemdescr}
\rSec1[locale.categories]{Standard \tcode{locale} categories}
\pnum
Each of the standard categories includes a family of facets.
Some of these implement formatting or parsing of a datum, for use
by standard or users' iostream operators
\tcode{\shl}
and
\tcode{\shr},
as members
\tcode{put()}
and
\tcode{get()},
respectively.
Each such member function takes an
\indexlibrary{\idxcode{flags}!\idxcode{ios_base}}%
\tcode{ios_base\&}
argument whose members
\indexlibrary{\idxcode{flags}!\idxcode{ios_base}}%
\tcode{flags()},
\indexlibrary{\idxcode{precision}!\idxcode{ios_base}}%
\tcode{precision()},
and
\indexlibrary{\idxcode{width}!\idxcode{ios_base}}%
\tcode{width()},
specify the format of the corresponding datum~(\ref{ios.base}).
Those functions which need to use other facets call its member
\tcode{getloc()}
to retrieve the locale imbued there.
Formatting facets use the character argument
\tcode{fill}
to fill out the specified width where necessary.
\pnum
The
\tcode{put()}
members make no provision for error reporting.
(Any failures of the
OutputIterator argument must be extracted from the returned iterator.)
The
\tcode{get()}
members take an
\tcode{ios_base::iostate\&}
argument whose value they ignore, but set to
\tcode{ios_base::failbit}
in case of a parse error.
\pnum
Within this clause it is unspecified whether one virtual function calls another
virtual function.
\rSec2[category.ctype]{The \tcode{ctype} category}
\indexlibrary{\idxcode{ctype_base}}%
\begin{codeblock}
namespace std {
class ctype_base {
public:
typedef @\textbf{\textit{T}}@ mask;
// numeric values are for exposition only.
static const mask space = 1 << 0;
static const mask print = 1 << 1;
static const mask cntrl = 1 << 2;
static const mask upper = 1 << 3;
static const mask lower = 1 << 4;
static const mask alpha = 1 << 5;
static const mask digit = 1 << 6;
static const mask punct = 1 << 7;
static const mask xdigit = 1 << 8;
static const mask blank = 1 << 9;
static const mask alnum = alpha | digit;
static const mask graph = alnum | punct;
};
}
\end{codeblock}
\pnum
The type
\tcode{mask}
is a bitmask type~(\ref{bitmask.types}).
\rSec3[locale.ctype]{Class template \tcode{ctype}}
\indexlibrary{\idxcode{ctype}}%
\begin{codeblock}
namespace std {
template <class charT>
class ctype : public locale::facet, public ctype_base {
public:
typedef charT char_type;
explicit ctype(size_t refs = 0);
bool is(mask m, charT c) const;
const charT* is(const charT* low, const charT* high, mask* vec) const;
const charT* scan_is(mask m,
const charT* low, const charT* high) const;
const charT* scan_not(mask m,
const charT* low, const charT* high) const;
charT toupper(charT c) const;
const charT* toupper(charT* low, const charT* high) const;
charT tolower(charT c) const;
const charT* tolower(charT* low, const charT* high) const;
charT widen(char c) const;
const char* widen(const char* low, const char* high, charT* to) const;
char narrow(charT c, char dfault) const;
const charT* narrow(const charT* low, const charT*, char dfault,
char* to) const;
static locale::id id;
protected:
~ctype();
virtual bool do_is(mask m, charT c) const;
virtual const charT* do_is(const charT* low, const charT* high,
mask* vec) const;
virtual const charT* do_scan_is(mask m,
const charT* low, const charT* high) const;
virtual const charT* do_scan_not(mask m,
const charT* low, const charT* high) const;
virtual charT do_toupper(charT) const;
virtual const charT* do_toupper(charT* low, const charT* high) const;
virtual charT do_tolower(charT) const;
virtual const charT* do_tolower(charT* low, const charT* high) const;
virtual charT do_widen(char) const;
virtual const char* do_widen(const char* low, const char* high,
charT* dest) const;
virtual char do_narrow(charT, char dfault) const;
virtual const charT* do_narrow(const charT* low, const charT* high,
char dfault, char* dest) const;
};
}
\end{codeblock}
\pnum
Class
\tcode{ctype}
encapsulates the C library
\tcode{<cctype>}
features.
\tcode{istream}
members are required to use
\tcode{ctype<>}
for character classing during input parsing.
\pnum
The specializations required in Table~\ref{tab:localization.category.facets}~(\ref{locale.category}), namely
\tcode{ctype<char>}
and
\tcode{ctype<wchar_t>},
implement character classing appropriate
to the implementation's native character set.
\rSec4[locale.ctype.members]{\tcode{ctype} members}
\indexlibrary{\idxcode{ctype}!\idxcode{is}}%
\indexlibrary{\idxcode{is}!\idxcode{ctype}}%
\begin{itemdecl}
bool is(mask m, charT c) const;
const charT* is(const charT* low, const charT* high,
mask* vec) const;
\end{itemdecl}
\begin{itemdescr}
\pnum
\returns
\tcode{do_is(m,c)}
or
\tcode{do_is(low,high,vec)}
\end{itemdescr}
\indexlibrary{\idxcode{ctype}!\idxcode{scan_is}}%
\indexlibrary{\idxcode{scan_is}!\idxcode{ctype}}%
\begin{itemdecl}
const charT* scan_is(mask m,
const charT* low, const charT* high) const;
\end{itemdecl}
\begin{itemdescr}
\pnum
\returns
\tcode{do_scan_is(m,low,high)}
\end{itemdescr}
\indexlibrary{\idxcode{ctype}!\idxcode{scan_not}}%
\indexlibrary{\idxcode{scan_not}!\idxcode{ctype}}%
\begin{itemdecl}
const charT* scan_not(mask m,
const charT* low, const charT* high) const;
\end{itemdecl}
\begin{itemdescr}
\pnum
\returns
\tcode{do_scan_not(m,low,high)}
\end{itemdescr}
\indexlibrary{\idxcode{ctype}!\idxcode{toupper}}%
\indexlibrary{\idxcode{toupper}!\idxcode{ctype}}%
\begin{itemdecl}
charT toupper(charT) const;
const charT* toupper(charT* low, const charT* high) const;
\end{itemdecl}
\begin{itemdescr}
\pnum
\returns
\tcode{do_toupper(c)}
or
\tcode{do_toupper(low,high)}
\end{itemdescr}
\indexlibrary{\idxcode{ctype}!\idxcode{tolower}}%
\indexlibrary{\idxcode{tolower}!\idxcode{ctype}}%
\begin{itemdecl}
charT tolower(charT c) const;
const charT* tolower(charT* low, const charT* high) const;
\end{itemdecl}
\begin{itemdescr}
\pnum
\returns
\tcode{do_tolower(c)}
or
\tcode{do_tolower(low,high)}
\end{itemdescr}
\indexlibrary{\idxcode{ctype}!\idxcode{widen}}%
\indexlibrary{\idxcode{widen}!\idxcode{ctype}}%
\begin{itemdecl}
charT widen(char c) const;
const char* widen(const char* low, const char* high, charT* to) const;
\end{itemdecl}
\begin{itemdescr}
\pnum
\returns
\tcode{do_widen(c)}
or
\tcode{do_widen(low,high,to)}
\end{itemdescr}
\indexlibrary{\idxcode{ctype}!\idxcode{narrow}}%
\indexlibrary{\idxcode{narrow}!\idxcode{ctype}}%
\begin{itemdecl}
char narrow(charT c, char dfault) const;
const charT* narrow(const charT* low, const charT*, char dfault,
char* to) const;
\end{itemdecl}
\begin{itemdescr}
\pnum
\returns
\tcode{do_narrow(c,dfault)}
or
\tcode{do_narrow(low,high,dfault,to)}
\end{itemdescr}
\rSec4[locale.ctype.virtuals]{\tcode{ctype} virtual functions}
\indexlibrary{\idxcode{ctype}!\idxcode{do_is}}%
\indexlibrary{\idxcode{do_is}!\idxcode{ctype}}%
\begin{itemdecl}
bool do_is(mask m, charT c) const;
const charT* do_is(const charT* low, const charT* high,
mask* vec) const;
\end{itemdecl}
\begin{itemdescr}
\pnum
\effects
Classifies a character or sequence of characters.
For each argument character, identifies a value
\tcode{M}
of type
\tcode{ctype_base::mask}.
The second form identifies a value \tcode{M} of type
\tcode{ctype_base::mask}
for each
\tcode{*p}
where
\tcode{(low<=p \&\& p<high)},
and places it into
\tcode{vec[p-low]}.
\pnum
\returns
The first form returns the result of the expression
\tcode{(M \& m) != 0};
i.e.,
\tcode{true}
if the character has the characteristics specified.
The second form returns \tcode{high}.
\end{itemdescr}
\indexlibrary{\idxcode{ctype_base}!\idxcode{do_scan_is}}%
\indexlibrary{\idxcode{do_scan_is}!\idxcode{ctype_base}}%
\begin{itemdecl}
const charT* do_scan_is(mask m,
const charT* low, const charT* high) const;
\end{itemdecl}
\begin{itemdescr}
\pnum
\effects
Locates a character in a buffer that conforms to a classification
\tcode{m}.
\pnum
\returns
The smallest pointer \tcode{p} in the range
\tcode{[low, high)}
such that
\tcode{is(m,*p)}
would return
\tcode{true};
otherwise, returns \tcode{high}.
\end{itemdescr}
\indexlibrary{\idxcode{ctype}!\idxcode{do_scan_not}}%
\indexlibrary{\idxcode{do_scan_not}!\idxcode{ctype}}%
\begin{itemdecl}
const charT* do_scan_not(mask m,
const charT* low, const charT* high) const;
\end{itemdecl}
\begin{itemdescr}
\pnum
\effects
Locates a character in a buffer that fails to conform to a classification
\tcode{m}.
\pnum
\returns
The smallest pointer \tcode{p}, if any, in the range
\range{low}{high}
such that
\tcode{is(m,*p)}
would return
\tcode{false};
otherwise, returns \tcode{high}.
\end{itemdescr}
\indexlibrary{\idxcode{ctype}!\idxcode{do_toupper}}%
\indexlibrary{\idxcode{do_toupper}!\idxcode{ctype}}%
\begin{itemdecl}
charT do_toupper(charT c) const;
const charT* do_toupper(charT* low, const charT* high) const;
\end{itemdecl}
\begin{itemdescr}
\pnum
\effects
Converts a character or characters to upper case.
The second form replaces each character
\tcode{*p}
in the range
\range{low}{high}
for which a corresponding upper-case character exists, with
that character.
\pnum
\returns
The first form returns the corresponding upper-case character if it
is known to exist, or its argument if not.
The second form returns \tcode{high}.
\end{itemdescr}
\indexlibrary{\idxcode{ctype}!\idxcode{do_tolower}}%
\indexlibrary{\idxcode{do_tolower}!\idxcode{ctype}}%
\begin{itemdecl}
charT do_tolower(charT c) const;
const charT* do_tolower(charT* low, const charT* high) const;
\end{itemdecl}
\begin{itemdescr}
\pnum
\effects
Converts a character or characters to lower case.
The second form replaces each character
\tcode{*p}
in the range
\range{low}{high}
and for which a corresponding lower-case character exists,
with that character.
\pnum
\returns
The first form returns the corresponding lower-case character if it
is known to exist, or its argument if not.
The second form returns \tcode{high}.
\end{itemdescr}
\indexlibrary{\idxcode{ctype}!\idxcode{do_widen}}%
\indexlibrary{\idxcode{do_widen}!\idxcode{ctype}}%
\begin{itemdecl}
charT do_widen(char c) const;
const char* do_widen(const char* low, const char* high,
charT* dest) const;
\end{itemdecl}
\begin{itemdescr}
\pnum
\effects
Applies the simplest reasonable transformation from a
\tcode{char}
value or sequence of
\tcode{char}
values to the corresponding
\tcode{charT}
value or values.\footnote{The char argument of
\tcode{do_widen}
is intended to accept values derived from character literals for conversion
to the locale's encoding.}
The only characters for which unique transformations are required
are those in the basic source character set~(\ref{lex.charset}).
For any named
\tcode{ctype}
category with a
\tcode{ctype<charT>}
facet \tcode{ctc} and valid
\tcode{ctype_base::mask}
value \tcode{M},
\tcode{(ctc.\brk{}is(M, c) || !is(M, do_widen(c)) )}
is
\tcode{true}.\footnote{In other words, the transformed character is not a member
of any character classification that \tcode{c} is not also a member of.}
The second form transforms each character
\tcode{*p}
in the range
\range{low}{high},
placing the result in
\tcode{dest[p-low]}.
\pnum
\returns
The first form returns the transformed value.
The second form returns \tcode{high}.
\end{itemdescr}
\indexlibrary{\idxcode{ctype}!\idxcode{do_narrow}}%
\indexlibrary{\idxcode{do_narrow}!\idxcode{ctype}}%
\begin{itemdecl}
char do_narrow(charT c, char dfault) const;
const charT* do_narrow(const charT* low, const charT* high,
char dfault, char* dest) const;
\end{itemdecl}
\begin{itemdescr}
\pnum
\effects
Applies the simplest reasonable transformation from a
\tcode{charT}
value or sequence of
\tcode{charT}
values to the corresponding
\tcode{char}
value or values.
For any character \tcode{c} in the basic source character set~(\ref{lex.charset})
the transformation is such that
\begin{codeblock}
do_widen(do_narrow(c,0)) == c
\end{codeblock}
For any named
\tcode{ctype}
category with a
\tcode{ctype<char>}
facet \tcode{ctc} however, and
\tcode{ctype_base::mask}
value \tcode{M},
\begin{codeblock}
(is(M,c) || !ctc.is(M, do_narrow(c,dfault)) )
\end{codeblock}
is
\tcode{true}
(unless
\tcode{do_narrow}
returns
\tcode{dfault}).
In addition, for any digit character \tcode{c},
the expression
\tcode{(do_narrow(c, dfault) - '0')}
evaluates to the digit value of the character.
The second form transforms each character
\tcode{*p}
in the range
\range{low}{high},
placing the result (or \tcode{dfault}
if no simple transformation is readily available) in
\tcode{dest[p-low]}.
\pnum
\returns
The first form returns the transformed value; or \tcode{dfault}
if no mapping is readily available.
The second form returns \tcode{high}.
\end{itemdescr}
\rSec3[locale.ctype.byname]{Class template \tcode{ctype_byname}}
\indexlibrary{\idxcode{ctype_byname}}%
\begin{codeblock}
namespace std {
template <class charT>
class ctype_byname : public ctype<charT> {
public:
typedef typename ctype<charT>::mask mask;
explicit ctype_byname(const char*, size_t refs = 0);
explicit ctype_byname(const string&, size_t refs = 0);
protected:
~ctype_byname();
};
}
\end{codeblock}
\rSec3[facet.ctype.special]{\tcode{ctype} specializations}
\indexlibrary{\idxcode{ctype<char>}}%
\begin{codeblock}
namespace std {
template <> class ctype<char>
: public locale::facet, public ctype_base {
public:
typedef char char_type;
explicit ctype(const mask* tab = 0, bool del = false,
size_t refs = 0);
bool is(mask m, char c) const;
const char* is(const char* low, const char* high, mask* vec) const;
const char* scan_is (mask m,
const char* low, const char* high) const;
const char* scan_not(mask m,
const char* low, const char* high) const;
char toupper(char c) const;
const char* toupper(char* low, const char* high) const;
char tolower(char c) const;
const char* tolower(char* low, const char* high) const;
char widen(char c) const;
const char* widen(const char* low, const char* high, char* to) const;
char narrow(char c, char dfault) const;
const char* narrow(const char* low, const char* high, char dfault,
char* to) const;
static locale::id id;
static const size_t table_size = @\impdef@;
const mask* table() const noexcept;
static const mask* classic_table() noexcept;
protected:
~ctype();
virtual char do_toupper(char c) const;
virtual const char* do_toupper(char* low, const char* high) const;
virtual char do_tolower(char c) const;
virtual const char* do_tolower(char* low, const char* high) const;
virtual char do_widen(char c) const;
virtual const char* do_widen(const char* low,
const char* high,
char* to) const;
virtual char do_narrow(char c, char dfault) const;
virtual const char* do_narrow(const char* low,
const char* high,
char dfault, char* to) const;
};
}
\end{codeblock}
\pnum
A specialization
\tcode{ctype<char>}
is provided so that the member functions on type
\tcode{char}
can be implemented
\tcode{inline}.\footnote{Only the
\tcode{char}
(not
\tcode{unsigned char}
and
\tcode{signed char})
form is provided.
The specialization is specified in the standard, and not left as an
implementation detail, because it affects the derivation interface for
\tcode{ctype<char>}.}
The \impldef{value of \tcode{ctype<char>::table_size}} value of member
\tcode{table_size}
is at least 256.
\rSec4[facet.ctype.char.dtor]{\tcode{ctype<char>} destructor}
\indexlibrary{\idxcode{ctype<char>}!destructor}%
\begin{itemdecl}
~ctype();
\end{itemdecl}
\begin{itemdescr}
\pnum
\effects
If the constructor's first argument was nonzero, and its second argument
was true, does
\tcode{delete [] table()}.
\end{itemdescr}
\rSec4[facet.ctype.char.members]{\tcode{ctype<char>} members}
\pnum
\indexlibrary{\idxcode{ctype<char>}!\idxcode{ctype<char>}}%
In the following member descriptions, for
\tcode{unsigned char}
values \tcode{v} where \tcode{v >= table_size},
\tcode{table()[v]} is assumed to have an
implementation-specific value (possibly different for each
such value \tcode{v}) without performing the array lookup.
\indexlibrary{\idxcode{ctype<char>}!constructor}%
\begin{itemdecl}
explicit ctype(const mask* tbl = 0, bool del = false,
size_t refs = 0);
\end{itemdecl}
\begin{itemdescr}
\pnum
\precondition
\tcode{tbl} either 0 or an array of at least
\tcode{table_size}
elements.
\pnum
\effects
Passes its \tcode{refs} argument to its base class constructor.
\end{itemdescr}
\indexlibrary{\idxcode{ctype<char>}!\idxcode{is}}%
\indexlibrary{\idxcode{is}!\idxcode{ctype<char>}}%
\begin{itemdecl}
bool is(mask m, char c) const;
const char* is(const char* low, const char* high,
mask* vec) const;
\end{itemdecl}
\begin{itemdescr}
\pnum
\effects
The second form, for all
\tcode{*p}
in the range
\range{low}{high},
assigns
into
\tcode{vec[p-low]}
the value
\tcode{table()[\,(un\-signed char)*p]}.
\pnum
\returns
The first form returns
\tcode{table()[(unsigned char)c] \& m};
the second form returns \tcode{high}.
\end{itemdescr}
\indexlibrary{\idxcode{ctype<char>}!\idxcode{scan_is}}%
\indexlibrary{\idxcode{scan_is}!\idxcode{ctype<char>}}%
\begin{itemdecl}
const char* scan_is(mask m,
const char* low, const char* high) const;
\end{itemdecl}
\begin{itemdescr}
\pnum
\returns
The smallest
\tcode{p}
in the range
\range{low}{high}
such that
\begin{codeblock}
table()[(unsigned char) *p] & m
\end{codeblock}
is
\tcode{true}.
\end{itemdescr}
\indexlibrary{\idxcode{ctype<char>}!\idxcode{scan_not}}%
\indexlibrary{\idxcode{scan_not}!\idxcode{ctype<char>}}%
\begin{itemdecl}
const char* scan_not(mask m,
const char* low, const char* high) const;
\end{itemdecl}
\begin{itemdescr}
\pnum
\returns
The smallest
\tcode{p}
in the range
\range{low}{high}
such that
\begin{codeblock}
table()[(unsigned char) *p] & m
\end{codeblock}
is
\tcode{false}.
\end{itemdescr}
\indexlibrary{\idxcode{ctype<char>}!\idxcode{toupper}}%
\indexlibrary{\idxcode{toupper}!\idxcode{ctype<char>}}%
\begin{itemdecl}
char toupper(char c) const;
const char* toupper(char* low, const char* high) const;
\end{itemdecl}
\begin{itemdescr}
\pnum
\returns
\tcode{do_toupper(c)}
or
\tcode{do_toupper(low,high)},
respectively.
\end{itemdescr}
\indexlibrary{\idxcode{ctype<char>}!\idxcode{tolower}}%
\indexlibrary{\idxcode{tolower}!\idxcode{ctype<char>}}%
\begin{itemdecl}
char tolower(char c) const;
const char* tolower(char* low, const char* high) const;
\end{itemdecl}
\begin{itemdescr}
\pnum
\returns
\tcode{do_tolower(c)}
or
\tcode{do_tolower(low,high)},
respectively.
\end{itemdescr}
\indexlibrary{\idxcode{ctype<char>}!\idxcode{widen}}%
\indexlibrary{\idxcode{widen}!\idxcode{ctype<char>}}%
\begin{itemdecl}
char widen(char c) const;
const char* widen(const char* low, const char* high,
char* to) const;
\end{itemdecl}
\begin{itemdescr}
\pnum
\returns
\tcode{do_widen(c)}
or
\indexlibrary{\idxcode{do_widen}}%
\tcode{do_widen(low, high, to)},
respectively.
\end{itemdescr}
\indexlibrary{\idxcode{ctype<char>}!\idxcode{narrow}}%
\indexlibrary{\idxcode{narrow}!\idxcode{ctype<char>}}%
\begin{itemdecl}
char narrow(char c, char dfault) const;
const char* narrow(const char* low, const char* high,
char dfault, char* to) const;
\end{itemdecl}
\begin{itemdescr}
\pnum
\returns
\indexlibrary{\idxcode{do_narrow}}%
\tcode{do_narrow(c, dfault)}
or
\indexlibrary{\idxcode{do_narrow}}%
\tcode{do_narrow(low, high, dfault, to)},
respectively.
\end{itemdescr}
\indexlibrary{\idxcode{ctype<char>}!\idxcode{table}}%
\indexlibrary{\idxcode{table}!\idxcode{ctype<char>}}%
\begin{itemdecl}
const mask* table() const noexcept;
\end{itemdecl}
\begin{itemdescr}
\pnum
\returns
The first constructor argument, if it was non-zero, otherwise
\tcode{classic_table()}.
\end{itemdescr}
\rSec4[facet.ctype.char.statics]{\tcode{ctype<char>} static members}
\indexlibrary{\idxcode{ctype<char>}!\idxcode{classic_table}}%
\indexlibrary{\idxcode{classic_table}!\idxcode{ctype<char>}}%
\begin{itemdecl}
static const mask* classic_table() noexcept;
\end{itemdecl}
\begin{itemdescr}
\pnum
\returns
A pointer to the initial element of an array of size
\tcode{table_size}
which represents the classifications of characters in the "C" locale.
\end{itemdescr}
\rSec4[facet.ctype.char.virtuals]{\tcode{ctype<char>} virtual functions}
\indexlibrary{\idxcode{ctype<char>}!\idxcode{do_toupper}}%
\indexlibrary{\idxcode{do_toupper}!\idxcode{ctype<char>}}%
\indexlibrary{\idxcode{ctype<char>}!\idxcode{do_tolower}}%
\indexlibrary{\idxcode{do_tolower}!\idxcode{ctype<char>}}%
\indexlibrary{\idxcode{ctype<char>}!\idxcode{do_widen}}%
\indexlibrary{\idxcode{do_widen}!\idxcode{ctype<char>}}%
\indexlibrary{\idxcode{ctype<char>}!\idxcode{do_narrow}}%
\indexlibrary{\idxcode{do_narrow}!\idxcode{ctype<char>}}%
\begin{codeblock}
char do_toupper(char) const;
const char* do_toupper(char* low, const char* high) const;
char do_tolower(char) const;
const char* do_tolower(char* low, const char* high) const;
virtual char do_widen(char c) const;
virtual const char* do_widen(const char* low,
const char* high,
char* to) const;
virtual char do_narrow(char c, char dfault) const;
virtual const char* do_narrow(const char* low,
const char* high,
char dfault, char* to) const;
\end{codeblock}
These functions are described identically as those members of the
same name in the
\tcode{ctype}
class template~(\ref{locale.ctype.members}).
\rSec3[locale.codecvt]{Class template \tcode{codecvt}}
\indexlibrary{\idxcode{codecvt}}%
\begin{codeblock}
namespace std {
class codecvt_base {
public:
enum result { ok, partial, error, noconv };
};
template <class internT, class externT, class stateT>
class codecvt : public locale::facet, public codecvt_base {
public:
typedef internT intern_type;
typedef externT extern_type;
typedef stateT state_type;
explicit codecvt(size_t refs = 0);
result out(stateT& state,
const internT* from, const internT* from_end, const internT*& from_next,
externT* to, externT* to_end, externT*& to_next) const;
result unshift(stateT& state,
externT* to, externT* to_end, externT*& to_next) const;
result in(stateT& state,
const externT* from, const externT* from_end, const externT*& from_next,
internT* to, internT* to_end, internT*& to_next) const;
int encoding() const noexcept;
bool always_noconv() const noexcept;
int length(stateT&, const externT* from, const externT* end,
size_t max) const;
int max_length() const noexcept;
static locale::id id;
protected:
~codecvt();
virtual result do_out(stateT& state,
const internT* from, const internT* from_end, const internT*& from_next,
externT* to, externT* to_end, externT*& to_next) const;
virtual result do_in(stateT& state,
const externT* from, const externT* from_end, const externT*& from_next,
internT* to, internT* to_end, internT*& to_next) const;
virtual result do_unshift(stateT& state,
externT* to, externT* to_end, externT*& to_next) const;
virtual int do_encoding() const noexcept;
virtual bool do_always_noconv() const noexcept;
virtual int do_length(stateT&, const externT* from,
const externT* end, size_t max) const;
virtual int do_max_length() const noexcept;
};
}
\end{codeblock}
\pnum
The class
\tcode{codecvt<internT,externT,stateT>}
is for use when
converting from one character encoding to another, such as from wide characters
to multibyte characters or between wide character encodings such as
Unicode and EUC.
\pnum
The
\tcode{stateT}
argument selects the pair of character encodings being mapped between.
\pnum
The specializations required in Table~\ref{tab:localization.category.facets}~(\ref{locale.category})
convert the implementation-defined native character set.
\tcode{codecvt<char, char, mbstate_t>}
implements a degenerate conversion;
it does not convert at all.
The specialization \tcode{codecvt<char16_t,} \tcode{char, mbstate_t>}
converts between the UTF-16 and UTF-8 encoding schemes, and
the specialization \tcode{codecvt} \tcode{<char32_t, char, mbstate_t>}
converts between the UTF-32 and UTF-8 encoding schemes.
\tcode{codecvt<wchar_t,char,mbstate_t>}
converts between the native character sets for narrow and wide characters.
Specializations on
\tcode{mbstate_t}
perform conversion between encodings known to the library implementer.
Other encodings can be converted by specializing on a user-defined
\tcode{stateT}
type.
The
\tcode{stateT}
object can contain any state that is useful to communicate to or from
the specialized
\tcode{do_in}
or
\tcode{do_out}
members.
\rSec4[locale.codecvt.members]{\tcode{codecvt} members}
\indexlibrary{\idxcode{codecvt}!\idxcode{out}}%
\indexlibrary{\idxcode{out}!\idxcode{codecvt}}%
\begin{itemdecl}
result out(stateT& state,
const internT* from, const internT* from_end, const internT*& from_next,
externT* to, externT* to_end, externT*& to_next) const;
\end{itemdecl}
\begin{itemdescr}
\pnum
\returns
\tcode{do_out(state, from, from_end, from_next, to, to_end, to_next)}
\end{itemdescr}
\indexlibrary{\idxcode{codecvt}!\idxcode{unshift}}%
\indexlibrary{\idxcode{unshift}!\idxcode{codecvt}}%
\begin{itemdecl}
result unshift(stateT& state,
externT* to, externT* to_end, externT*& to_next) const;
\end{itemdecl}
\begin{itemdescr}
\pnum
\returns
\tcode{do_unshift(state, to, to_end, to_next)}
\end{itemdescr}
\indexlibrary{\idxcode{codecvt}!\idxcode{in}}%
\indexlibrary{\idxcode{in}!\idxcode{codecvt}}%
\begin{itemdecl}
result in(stateT& state,
const externT* from, const externT* from_end, const externT*& from_next,
internT* to, internT* to_end, internT*& to_next) const;
\end{itemdecl}
\begin{itemdescr}
\pnum
\returns
\tcode{do_in(state, from, from_end, from_next, to, to_end, to_next)}
\end{itemdescr}
\indexlibrary{\idxcode{codecvt}!\idxcode{encoding}}%
\indexlibrary{\idxcode{encoding}!\idxcode{codecvt}}%
\begin{itemdecl}
int encoding() const noexcept;
\end{itemdecl}
\begin{itemdescr}
\pnum
\returns
\tcode{do_encoding()}
\end{itemdescr}
\indexlibrary{\idxcode{codecvt}!\idxcode{always_noconv}}%
\indexlibrary{\idxcode{always_noconv}!\idxcode{codecvt}}%
\begin{itemdecl}
bool always_noconv() const noexcept;
\end{itemdecl}
\begin{itemdescr}
\pnum
\returns
\tcode{do_always_noconv()}
\end{itemdescr}
\indexlibrary{\idxcode{codecvt}!\idxcode{length}}%
\indexlibrary{\idxcode{length}!\idxcode{codecvt}}%
\begin{itemdecl}
int length(stateT& state, const externT* from, const externT* from_end,
size_t max) const;
\end{itemdecl}
\begin{itemdescr}
\pnum
\returns
\tcode{do_length(state, from,from_end,max)}
\end{itemdescr}
\indexlibrary{\idxcode{codecvt}!\idxcode{max_length}}%
\indexlibrary{\idxcode{max_length}!\idxcode{codecvt}}%
\begin{itemdecl}
int max_length() const noexcept;
\end{itemdecl}
\begin{itemdescr}
\pnum
\returns
\tcode{do_max_length()}
\end{itemdescr}
\rSec4[locale.codecvt.virtuals]{\tcode{codecvt} virtual functions}
\indexlibrary{\idxcode{codecvt}!\idxcode{do_out}}%
\indexlibrary{\idxcode{do_out}!\idxcode{codecvt}}%
\indexlibrary{\idxcode{codecvt}!\idxcode{do_in}}%
\indexlibrary{\idxcode{do_in}!\idxcode{codecvt}}%
\begin{itemdecl}
result do_out(stateT& state,
const internT* from, const internT* from_end, const internT*& from_next,
externT* to, externT* to_end, externT*& to_next) const;
result do_in(stateT& state,
const externT* from, const externT* from_end, const externT*& from_next,
internT* to, internT* to_end, internT*& to_next) const;
\end{itemdecl}
\begin{itemdescr}
\pnum
\preconditions
\tcode{(from<=from_end \&\& to<=to_end)}
well-defined and
\tcode{true};
\tcode{state} initialized, if at the beginning of a sequence, or else equal to
the result of converting the preceding characters in the sequence.
\pnum
\effects
Translates characters in the source range
\tcode{[from,from_end)},
placing the results in sequential positions starting at destination \tcode{to}.
Converts no more than
\tcode{(from_end-from)}
source elements, and
stores no more than
\tcode{(to_end-to)}
destination elements.
Stops if it encounters a character it cannot convert.
It always leaves the \tcode{from_next} and \tcode{to_next} pointers
pointing one beyond the last element successfully converted.
If returns
\tcode{noconv},
\tcode{internT}
and
\tcode{externT}
are the same type and the converted sequence is
identical to the input sequence
\tcode{[from, from_next)}.
\tcode{to_next} is set equal to \tcode{to}, the value of \tcode{state} is
unchanged, and there are no changes to the values in
\tcode{[to, to_end)}.
\pnum
A
\tcode{codecvt}
facet that is used by
\tcode{basic_filebuf}~(\ref{file.streams}) shall have the property that if
\begin{codeblock}
do_out(state, from, from_end, from_next, to, to_end, to_next)
\end{codeblock}
would return
\tcode{ok},
where
\tcode{from != from_end},
then
\begin{codeblock}
do_out(state, from, from + 1, from_next, to, to_end, to_next)
\end{codeblock}
shall also return
\tcode{ok},
and that if
\begin{codeblock}
do_in(state, from, from_end, from_next, to, to_end, to_next)
\end{codeblock}
would return
\tcode{ok},
where
\tcode{to != to_end},
then
\begin{codeblock}
do_in(state, from, from_end, from_next, to, to + 1, to_next)
\end{codeblock}
shall also return
\tcode{ok}.\footnote{Informally, this means that
\tcode{basic_filebuf}
assumes that the mappings from internal to external characters is
1 to N: a
\tcode{codecvt}
facet that is used by
\tcode{basic_filebuf}
must be able to translate characters one internal character at a time.
}
\enternote As a result of operations on \tcode{state}, it can return \tcode{ok} or \tcode{partial} and set \tcode{from_next == from} and \tcode{to_next != to}. \exitnote
\pnum
\notes
Its operations on \tcode{state} are unspecified.
\enternote
This argument can be used, for example, to maintain
shift state, to specify conversion options (such as count only), or to
identify a cache of seek offsets.
\exitnote
\pnum
\returns
An enumeration value, as summarized in Table~\ref{tab:localization.convert.result.values.out.in}.
\begin{floattable}{\tcode{do_in/do_out} result values}{tab:localization.convert.result.values.out.in}
{lp{3in}}
\topline
\lhdr{Value} & \rhdr{Meaning} \\ \capsep
\tcode{ok} & completed the conversion \\
\tcode{partial} & not all source characters converted \\
\tcode{error} &
encountered a character in \tcode{[from,from_end)}
that it could not convert \\
\tcode{noconv} &
\tcode{internT} and \tcode{externT} are the same type, and input
sequence is identical to converted sequence \\
\end{floattable}
A return value of
\tcode{partial},
if
\tcode{(from_next==from_end)},
indicates that either the destination sequence has not absorbed all the
available destination elements, or that additional source elements are
needed before another destination element can be produced.
\end{itemdescr}
\indexlibrary{\idxcode{codecvt}!\idxcode{do_unshift}}%
\indexlibrary{\idxcode{do_unshift}!\idxcode{codecvt}}%
\begin{itemdecl}
result do_unshift(stateT& state,
externT* to, externT* to_end, externT*& to_next) const;
\end{itemdecl}
\begin{itemdescr}
\pnum
\requires
\tcode{(to <= to_end)}
well defined and true; state initialized, if at the beginning of a sequence,
or else equal to the result of converting the preceding characters in the
sequence.
\pnum
\effects
Places characters starting at \tcode{to} that should be appended
to terminate a sequence when the current
\tcode{stateT}
is given by \tcode{state}.\footnote{Typically these will be characters to return the state to
\tcode{stateT()}}
Stores no more than
\tcode{(to_end-to)}
destination elements, and leaves the \tcode{to_next} pointer
pointing one beyond the last element successfully stored.
\pnum
\returns
An enumeration value, as summarized in Table~\ref{tab:localization.convert.result.values.unshift}.
\begin{floattable}{\tcode{do_unshift} result values}{tab:localization.convert.result.values.unshift}
{lp{.50\hsize}}
\topline
\lhdr{Value} & \rhdr{Meaning} \\ \capsep
\tcode{ok} & completed the sequence \\
\tcode{partial} &
space for more than \tcode{to_end-to} destination elements was needed
to terminate a sequence given the value of \tcode{state}\\
\tcode{error} & an unspecified error has occurred \\
\tcode{noconv} & no termination is needed for this \tcode{state_type} \\
\end{floattable}
\end{itemdescr}
\indexlibrary{\idxcode{codecvt}!\idxcode{do_encoding}}%
\indexlibrary{\idxcode{do_encoding}!\idxcode{codecvt}}%
\begin{itemdecl}
int do_encoding() const noexcept;
\end{itemdecl}
\begin{itemdescr}
\pnum
\returns
-1 if the encoding of the \tcode{externT} sequence is state-dependent; else the
constant number of \tcode{externT} characters needed to produce an internal
character; or 0 if this number is not a constant.\footnote{If \tcode{encoding()}
yields -1, then more than \tcode{max_length()} \tcode{externT} elements
may be consumed when producing a single \tcode{internT} character, and additional
\tcode{externT} elements may appear at the end of a sequence after those that
yield the final \tcode{internT} character.}
\end{itemdescr}
\indexlibrary{\idxcode{codecvt}!\idxcode{do_always_noconv}}%
\indexlibrary{\idxcode{do_always_noconv}!\idxcode{codecvt}}%
\begin{itemdecl}
bool do_always_noconv() const noexcept;
\end{itemdecl}
\begin{itemdescr}
\pnum
\returns
\tcode{true}
if
\tcode{do_in()}
and
\tcode{do_out()}
return
\tcode{noconv}
for all valid argument values.
\tcode{codecvt<char, char, mbstate_t>}
returns
\tcode{true}.
\end{itemdescr}
\indexlibrary{\idxcode{codecvt}!\idxcode{do_length}}%
\indexlibrary{\idxcode{do_length}!\idxcode{codecvt}}%
\begin{itemdecl}
int do_length(stateT& state, const externT* from, const externT* from_end,
size_t max) const;
\end{itemdecl}
\begin{itemdescr}
\pnum
\preconditions
\tcode{(from<=from_end)}
well-defined and
\tcode{true};
\tcode{state} initialized, if at the beginning of a sequence, or else equal to
the result of converting the preceding characters in the sequence.
\pnum
\effects
The effect on the \tcode{state} argument is ``as if'' it called
\tcode{do_in(state, from, from_end, from, to, to+max, to)}
for \tcode{to} pointing to a buffer of at least \tcode{max} elements.
\pnum
\returns
\tcode{(from_next-from)}
where
\tcode{from_next}
is the largest value in the range
\tcode{[from,from_end]}
such that the sequence of values in the range
\range{from}{from_next}
represents
\tcode{max}
or fewer valid complete characters of type
\tcode{internT}.
The specialization
\tcode{codecvt<char, char, mbstate_t>},
returns the lesser of
\tcode{max}
and
\tcode{(from_end-from)}.
\end{itemdescr}
\indexlibrary{\idxcode{codecvt}!\idxcode{do_max_length}}%
\indexlibrary{\idxcode{do_max_length}!\idxcode{codecvt}}%
\begin{itemdecl}
int do_max_length() const noexcept;
\end{itemdecl}
\begin{itemdescr}
\pnum
\returns
The maximum value that
\tcode{do_length(state, from, from_end, 1)}
can return for any valid range
\tcode{[from, from_end)}
and
\tcode{stateT}
value
\tcode{state}.
The specialization
\tcode{codecvt<char, char, mbstate_t>\colcol{}do_max_length()}
returns 1.
\end{itemdescr}
\rSec3[locale.codecvt.byname]{Class template \tcode{codecvt_byname}}
\indexlibrary{\idxcode{codecvt_byname}}%
\begin{codeblock}
namespace std {
template <class internT, class externT, class stateT>
class codecvt_byname : public codecvt<internT, externT, stateT> {
public:
explicit codecvt_byname(const char*, size_t refs = 0);
explicit codecvt_byname(const string&, size_t refs = 0);
protected:
~codecvt_byname();
};
}
\end{codeblock}
\rSec2[category.numeric]{The numeric category}
\pnum
The classes
\tcode{num_get<>}
and
\tcode{num_put<>}
handle numeric formatting and parsing.
Virtual functions are provided for several numeric types.
Implementations may (but are not required to) delegate extraction
of smaller types to extractors for larger types.\footnote{Parsing
\tcode{"-1"} correctly into, e.g., an
\tcode{unsigned short}
requires that the corresponding member
\tcode{get()}
at least extract the sign before delegating.}
\pnum
All specifications of member functions for
\tcode{num_put}
and
\tcode{num_get}
in the subclauses of~\ref{category.numeric} only apply to the
specializations required in Tables~\ref{tab:localization.category.facets}
and~\ref{tab:localization.required.specializations}~(\ref{locale.category}), namely
\tcode{num_get<char>},
\tcode{num_get<wchar_t>},
\tcode{num_get<C, InputIterator>},
\tcode{num_put<char>},
\tcode{num_put<wchar_t>},
and
\tcode{num_put<C,OutputIterator>}.
These specializations refer to the
\tcode{ios_base\&}
argument for formatting specifications~(\ref{locale.categories}),
and to its imbued locale for the
\tcode{numpunct<>}
facet to identify all numeric punctuation preferences,
and also for the
\tcode{ctype<>}
facet to perform character classification.
\pnum
Extractor and inserter members of the standard iostreams use
\tcode{num_get<>}
and
\tcode{num_put<>}
member functions for formatting and parsing numeric values~(\ref{istream.formatted.reqmts}, \ref{ostream.formatted.reqmts}).
\rSec3[locale.num.get]{Class template \tcode{num_get}}
\indexlibrary{\idxcode{num_get}}%
\begin{codeblock}
namespace std {
template <class charT, class InputIterator = istreambuf_iterator<charT> >
class num_get : public locale::facet {
public:
typedef charT char_type;
typedef InputIterator iter_type;
explicit num_get(size_t refs = 0);
iter_type get(iter_type in, iter_type end, ios_base&,
ios_base::iostate& err, bool& v) const;
iter_type get(iter_type in, iter_type end, ios_base&,
ios_base::iostate& err, long& v) const;
iter_type get(iter_type in, iter_type end, ios_base&,
ios_base::iostate& err, long long& v) const;
iter_type get(iter_type in, iter_type end, ios_base&,
ios_base::iostate& err, unsigned short& v) const;
iter_type get(iter_type in, iter_type end, ios_base&,
ios_base::iostate& err, unsigned int& v) const;
iter_type get(iter_type in, iter_type end, ios_base&,
ios_base::iostate& err, unsigned long& v) const;
iter_type get(iter_type in, iter_type end, ios_base&,
ios_base::iostate& err, unsigned long long& v) const;
iter_type get(iter_type in, iter_type end, ios_base&,
ios_base::iostate& err, float& v) const;
iter_type get(iter_type in, iter_type end, ios_base&,
ios_base::iostate& err, double& v) const;
iter_type get(iter_type in, iter_type end, ios_base&,
ios_base::iostate& err, long double& v) const;
iter_type get(iter_type in, iter_type end, ios_base&,
ios_base::iostate& err, void*& v) const;
static locale::id id;
protected:
~num_get();
virtual iter_type do_get(iter_type, iter_type, ios_base&,
ios_base::iostate& err, bool& v) const;
virtual iter_type do_get(iter_type, iter_type, ios_base&,
ios_base::iostate& err, long& v) const;
virtual iter_type do_get(iter_type, iter_type, ios_base&,
ios_base::iostate& err, long long& v) const;
virtual iter_type do_get(iter_type, iter_type, ios_base&,
ios_base::iostate& err, unsigned short& v) const;
virtual iter_type do_get(iter_type, iter_type, ios_base&,
ios_base::iostate& err, unsigned int& v) const;
virtual iter_type do_get(iter_type, iter_type, ios_base&,
ios_base::iostate& err, unsigned long& v) const;
virtual iter_type do_get(iter_type, iter_type, ios_base&,
ios_base::iostate& err, unsigned long long& v) const;
virtual iter_type do_get(iter_type, iter_type, ios_base&,
ios_base::iostate& err, float& v) const;
virtual iter_type do_get(iter_type, iter_type, ios_base&,
ios_base::iostate& err, double& v) const;
virtual iter_type do_get(iter_type, iter_type, ios_base&,
ios_base::iostate& err, long double& v) const;
virtual iter_type do_get(iter_type, iter_type, ios_base&,
ios_base::iostate& err, void*& v) const;
};
}
\end{codeblock}
\pnum
The facet
\tcode{num_get}
is used to parse numeric values from an input sequence such as an istream.
\rSec4[facet.num.get.members]{\tcode{num_get} members}
\indexlibrary{\idxcode{num_get}!\idxcode{get}}%
\indexlibrary{\idxcode{get}!\idxcode{num_get}}%
\begin{itemdecl}
iter_type get(iter_type in, iter_type end, ios_base& str,
ios_base::iostate& err, bool& val) const;
iter_type get(iter_type in, iter_type end, ios_base& str,
ios_base::iostate& err, long& val) const;
iter_type get(iter_type in, iter_type end, ios_base& str,
ios_base::iostate& err, long long& val) const;
iter_type get(iter_type in, iter_type end, ios_base& str,
ios_base::iostate& err, unsigned short& val) const;
iter_type get(iter_type in, iter_type end, ios_base& str,
ios_base::iostate& err, unsigned int& val) const;
iter_type get(iter_type in, iter_type end, ios_base& str,
ios_base::iostate& err, unsigned long& val) const;
iter_type get(iter_type in, iter_type end, ios_base& str,
ios_base::iostate& err, unsigned long long& val) const;
iter_type get(iter_type in, iter_type end, ios_base& str,
ios_base::iostate& err, float& val) const;
iter_type get(iter_type in, iter_type end, ios_base& str,
ios_base::iostate& err, double& val) const;
iter_type get(iter_type in, iter_type end, ios_base& str,
ios_base::iostate& err, long double& val) const;
iter_type get(iter_type in, iter_type end, ios_base& str,
ios_base::iostate& err, void*& val) const;
\end{itemdecl}
\begin{itemdescr}
\pnum
\returns
\tcode{do_get(in, end, str, err, val)}.
\end{itemdescr}
\rSec4[facet.num.get.virtuals]{\tcode{num_get} virtual functions}
\indexlibrary{\idxcode{num_get}!\idxcode{do_get}}%
\indexlibrary{\idxcode{do_get}!\idxcode{num_get}}%
\begin{itemdecl}
iter_type do_get(iter_type in, iter_type end, ios_base& str,
ios_base::iostate& err, long& val) const;
iter_type do_get(iter_type in, iter_type end, ios_base& str,
ios_base::iostate& err, long long& val) const;
iter_type do_get(iter_type in, iter_type end, ios_base& str,
ios_base::iostate& err, unsigned short& val) const;
iter_type do_get(iter_type in, iter_type end, ios_base& str,
ios_base::iostate& err, unsigned int& val) const;
iter_type do_get(iter_type in, iter_type end, ios_base& str,
ios_base::iostate& err, unsigned long& val) const;
iter_type do_get(iter_type in, iter_type end, ios_base& str,
ios_base::iostate& err, unsigned long long& val) const;
iter_type do_get(iter_type in, iter_type end, ios_base& str,
ios_base::iostate& err, float& val) const;
iter_type do_get(iter_type in, iter_type end, ios_base& str,
ios_base::iostate& err, double& val) const;
iter_type do_get(iter_type in, iter_type end, ios_base& str,
ios_base::iostate& err, long double& val) const;
iter_type do_get(iter_type in, iter_type end, ios_base& str,
ios_base::iostate& err, void*& val) const;
\end{itemdecl}
\begin{itemdescr}
\pnum
\effects
Reads characters from \tcode{in},
interpreting them according to
\tcode{str.flags()},
\tcode{use_facet<ctype<\brk{}charT> >(loc)},
and
\tcode{use_facet< numpunct<charT> >(loc)},
where
\tcode{loc}
is
\tcode{str.getloc()}.
\pnum
The details of this operation occur in three stages
\begin{itemize}
\item
Stage 1:
Determine a conversion specifier
\item
Stage 2: Extract characters from \tcode{in} and determine a corresponding
\tcode{char}
value for the format expected by the conversion specification determined
in stage 1.
\item
Stage 3:
Store results
\end{itemize}
\pnum
The details of the stages are presented below.
\begin{description}
\stage{1}
The function initializes local variables via
\begin{codeblock}
fmtflags flags = str .flags();
fmtflags basefield = (flags & ios_base::basefield);
fmtflags uppercase = (flags & ios_base::uppercase);
fmtflags boolalpha = (flags & ios_base::boolalpha);
\end{codeblock}
For conversion to an integral type, the
function determines the integral conversion specifier as indicated in
Table~\ref{tab:localization.integer.conversions.in}.
The table is ordered.
That is, the first line whose condition is true applies.
\begin{floattable}{Integer conversions}{tab:localization.integer.conversions.in}
{lc}
\topline
\lhdr{State} & \tcode{stdio} equivalent \\ \capsep
\tcode{basefield == oct} & \tcode{\%o} \\ \rowsep
\tcode{basefield == hex} & \tcode{\%X} \\ \rowsep
\tcode{basefield == 0} & \tcode{\%i} \\ \capsep
\tcode{signed} integral type & \tcode{\%d} \\ \rowsep
\tcode{unsigned} integral type & \tcode{\%u} \\
\end{floattable}
For conversions to a floating type the specifier is
\tcode{\%g}.
For conversions to
\tcode{void*}
the specifier is
\tcode{\%p}.
A length modifier is added to the conversion specification, if needed,
as indicated in Table~\ref{tab:localization.length.modifier.in}.
\begin{floattable}{Length modifier}{tab:localization.length.modifier.in}
{lc}
\topline
\lhdr{Type} & Length modifier \\ \capsep
\tcode{short} & \tcode{h} \\ \rowsep
\tcode{unsigned short} & \tcode{h} \\ \rowsep
\tcode{long} & \tcode{l} \\ \rowsep
\tcode{unsigned long} & \tcode{l} \\ \rowsep
\tcode{long long} & \tcode{ll} \\ \rowsep
\tcode{unsigned long long} & \tcode{ll} \\ \rowsep
\tcode{double} & \tcode{l} \\ \rowsep
\tcode{long double} & \tcode{L} \\
\end{floattable}
\stage{2}
If
\tcode{in==end}
then stage 2 terminates.
Otherwise a
\tcode{charT}
is taken from \tcode{in} and local variables are initialized as if by
\begin{codeblock}
char_type ct = *in ;
char c = src[find(atoms, atoms + sizeof(src) - 1, ct) - atoms];
if (ct == use_facet<numpunct<charT> >(loc).decimal_point())
c = '.';
bool discard =
ct == use_facet<numpunct<charT> >(loc).thousands_sep()
&& use_facet<numpunct<charT> >(loc).grouping().length() != 0;
\end{codeblock}
where the values
\tcode{src}
and
\tcode{atoms}
are defined as if by:
\begin{codeblock}
static const char src[] = "0123456789abcdefxABCDEFX+-";
char_type atoms[sizeof(src)];
use_facet<ctype<charT> >(loc).widen(src, src + sizeof(src), atoms);
\end{codeblock}
for this value of
\tcode{loc}.
If \tcode{discard} is true, then if
\tcode{'.'}
has not yet been accumulated, then the position of the character is remembered,
but the character is otherwise ignored.
Otherwise, if
\tcode{'.'}
has already been accumulated, the character is discarded and
Stage 2 terminates.
If it is not discarded, then a check is made to determine if \tcode{c} is
allowed as the next character of an input field of the conversion specifier
returned by Stage 1. If so, it is accumulated.
If the character is either discarded or accumulated then \tcode{in}
is advanced by
\tcode{++in}
and processing returns to the beginning of stage 2.
\stage{3}
The sequence of \tcode{char}{s} accumulated in stage 2 (the field) is converted to a numeric value by the rules of one of the functions declared in the header \tcode{<cstdlib>}:
\begin{itemize}
\item For a signed integer value, the function \tcode{strtoll}.
\item For an unsigned integer value, the function \tcode{strtoull}.
\item For a floating-point value, the function \tcode{strtold}.
\end{itemize}
The numeric value to be stored can be one of:
\begin{itemize}
\item zero, if the conversion function fails to convert the entire field. \tcode{ios_base::failbit} is assigned to \tcode{err}.
\item the most positive representable value, if the field represents a value too large positive to be represented in \tcode{val}. \tcode{ios_base::failbit} is assigned to \tcode{err}.
\item the most negative representable value or zero for an unsigned integer type, if the field represents a value too large negative to be represented in \tcode{val}. \tcode{ios_base::failbit} is assigned to \tcode{err}.
\item the converted value, otherwise.
\end{itemize}
The resultant numeric value is stored in \tcode{val}.
\end{description}
\pnum
Digit grouping is checked.
That is, the positions of discarded
separators is examined for consistency with
\tcode{use_facet<numpunct<charT> >(loc).grouping()}.
If they are not consistent then
\tcode{ios_base::failbit}
is assigned to \tcode{err}.
\pnum
In any case, if stage 2 processing was terminated by the test for
\tcode{in==end}
then
\tcode{err |=ios_base::eofbit}
is performed.
\end{itemdescr}
\indexlibrary{\idxcode{do_get}!\idxcode{num_get}}%
\indexlibrary{\idxcode{num_get}!\idxcode{do_get}}%
\begin{itemdecl}
iter_type do_get(iter_type in, iter_type end, ios_base& str,
ios_base::iostate& err, bool& val) const;
\end{itemdecl}
\begin{itemdescr}
\pnum
\effects
If
\tcode{(str\tcode{.flags()\&ios_base::boolalpha)==0}}
then input proceeds as it would for a
\tcode{long}
except that if a value is being stored into \tcode{val},
the value is determined according to the following:
If the value to be stored is 0 then
\tcode{false}
is stored.
If the value is 1
then
\tcode{true}
is stored.
Otherwise \tcode{true} is stored and \tcode{ios_base::failbit} is assigned to \tcode{err}.
\pnum
Otherwise target sequences are determined ``as if'' by calling the
members
\tcode{falsename()}
and
\tcode{truename()}
of the facet obtained by
\tcode{use_facet<numpunct<charT> >(str.getloc())}.
Successive characters in the range
\range{in}{end}
(see~\ref{sequence.reqmts}) are obtained and
matched against corresponding positions in the target sequences only
as necessary to identify a unique match. The input iterator \tcode{in} is
compared to \tcode{end} only when necessary to obtain a character. If a target sequence is uniquely matched, \tcode{val} is set to the
corresponding value. Otherwise \tcode{false} is stored and \tcode{ios_base::failbit} is assigned to \tcode{err}.
\pnum
The \tcode{in} iterator is always left pointing one position beyond the last
character successfully matched. If \tcode{val} is set, then \tcode{err} is set to
\tcode{str.goodbit};
or to
\tcode{str.eofbit}
if, when seeking another character to match, it is found that
\tcode{(in == end)}.
If \tcode{val} is not set, then \tcode{err} is set to
\tcode{str.failbit};
or to
\tcode{(str.failbit|str.eofbit)}
if the reason for the failure was that
\tcode{(in == end)}.
\enterexample
For targets
\tcode{true}:
\tcode{"a"}
and
\tcode{false}:
\tcode{"abb"},
the input sequence
\tcode{"a"}
yields
\tcode{val == true}
and
\tcode{err == str.eofbit};
the input sequence
\tcode{"abc"}
yields
\tcode{err = str.failbit},
with \tcode{in} ending at the
\tcode{'c'}
element. For targets
\tcode{true}:
\tcode{"1"}
and
\tcode{false}:
\tcode{"0"}, the input sequence \tcode{"1"} yields
\tcode{val == true}
and
\tcode{err == str.goodbit}.
For empty targets \tcode{("")}, any input sequence yields
\tcode{err == str.failbit}.
\exitexample
\pnum
\returns
\tcode{in}.
\end{itemdescr}
\rSec3[locale.nm.put]{Class template \tcode{num_put}}
\indexlibrary{\idxcode{num_put}}%
\begin{codeblock}
namespace std {
template <class charT, class OutputIterator = ostreambuf_iterator<charT> >
class num_put : public locale::facet {
public:
typedef charT char_type;
typedef OutputIterator iter_type;
explicit num_put(size_t refs = 0);
iter_type put(iter_type s, ios_base& f, char_type fill, bool v) const;
iter_type put(iter_type s, ios_base& f, char_type fill, long v) const;
iter_type put(iter_type s, ios_base& f, char_type fill, long long v) const;
iter_type put(iter_type s, ios_base& f, char_type fill,
unsigned long v) const;
iter_type put(iter_type s, ios_base& f, char_type fill,
unsigned long long v) const;
iter_type put(iter_type s, ios_base& f, char_type fill,
double v) const;
iter_type put(iter_type s, ios_base& f, char_type fill,
long double v) const;
iter_type put(iter_type s, ios_base& f, char_type fill,
const void* v) const;
static locale::id id;
protected:
~num_put();
virtual iter_type do_put(iter_type, ios_base&, char_type fill,
bool v) const;
virtual iter_type do_put(iter_type, ios_base&, char_type fill,
long v) const;
virtual iter_type do_put(iter_type, ios_base&, char_type fill,
long long v) const;
virtual iter_type do_put(iter_type, ios_base&, char_type fill,
unsigned long) const;
virtual iter_type do_put(iter_type, ios_base&, char_type fill,
unsigned long long) const;
virtual iter_type do_put(iter_type, ios_base&, char_type fill,
double v) const;
virtual iter_type do_put(iter_type, ios_base&, char_type fill,
long double v) const;
virtual iter_type do_put(iter_type, ios_base&, char_type fill,
const void* v) const;
};
}
\end{codeblock}
\pnum
The facet
\tcode{num_put}
is used to format numeric values to a character sequence such as an ostream.
\rSec4[facet.num.put.members]{\tcode{num_put} members}
\indexlibrary{\idxcode{num_put}!\idxcode{put}}%
\indexlibrary{\idxcode{put}!\idxcode{num_put}}%
\begin{itemdecl}
iter_type put(iter_type out, ios_base& str, char_type fill,
bool val) const;
iter_type put(iter_type out, ios_base& str, char_type fill,
long val) const;
iter_type put(iter_type out, ios_base& str, char_type fill,
long long val) const;
iter_type put(iter_type out, ios_base& str, char_type fill,
unsigned long val) const;
iter_type put(iter_type out, ios_base& str, char_type fill,
unsigned long long val) const;
iter_type put(iter_type out, ios_base& str, char_type fill,
double val) const;
iter_type put(iter_type out, ios_base& str, char_type fill,
long double val) const;
iter_type put(iter_type out, ios_base& str, char_type fill,
const void* val) const;
\end{itemdecl}
\begin{itemdescr}
\pnum
\returns
\tcode{do_put(out, str, fill, val)}.
\end{itemdescr}
\rSec4[facet.num.put.virtuals]{\tcode{num_put} virtual functions}
\indexlibrary{\idxcode{num_put}!\idxcode{do_put}}%
\indexlibrary{\idxcode{do_put}!\idxcode{num_put}}%
\begin{itemdecl}
iter_type do_put(iter_type out, ios_base& str, char_type fill,
long val) const;
iter_type do_put(iter_type out, ios_base& str, char_type fill,
long long val) const;
iter_type do_put(iter_type out, ios_base& str, char_type fill,
unsigned long val) const;
iter_type do_put(iter_type out, ios_base& str, char_type fill,
unsigned long long val) const;
iter_type do_put(iter_type out, ios_base& str, char_type fill,
double val) const;
iter_type do_put(iter_type out, ios_base& str, char_type fill,
long double val) const;
iter_type do_put(iter_type out, ios_base& str, char_type fill,
const void* val) const;
\end{itemdecl}
\begin{itemdescr}
\pnum
\effects
Writes characters to the sequence \tcode{out},
formatting \tcode{val} as desired.
In the following description,
a local variable initialized with
\begin{codeblock}
locale loc = str.getloc();
\end{codeblock}
\pnum
The details of this operation occur in several stages:
\begin{itemize}
\item
Stage 1:
Determine a printf conversion specifier \tcode{spec} and
determining the characters that would be printed by
\tcode{printf}~(\ref{c.files})
given this conversion specifier for
\begin{codeblock}
printf(spec, val )
\end{codeblock}
assuming that the current locale is
the \tcode{"C"} locale.
\item
Stage 2:
Adjust the representation by converting each
\tcode{char}
determined by stage 1 to a
\tcode{charT}
using a conversion and values returned by members of
\tcode{use_facet< numpunct<charT> >(str.getloc())}
\item
Stage 3:
Determine where padding is required.
\item
Stage 4:
Insert the sequence into the \tcode{out}.
\end{itemize}
\pnum
Detailed descriptions of each stage follow.
\pnum
\returns
\tcode{out}.
\pnum
\begin{description}
\stage{1}
The first action of stage 1 is to determine a conversion specifier.
The tables that describe this determination use the following local variables
\begin{codeblock}
fmtflags flags = str.flags() ;
fmtflags basefield = (flags & (ios_base::basefield));
fmtflags uppercase = (flags & (ios_base::uppercase));
fmtflags floatfield = (flags & (ios_base::floatfield));
fmtflags showpos = (flags & (ios_base::showpos));
fmtflags showbase = (flags & (ios_base::showbase));
\end{codeblock}
All tables used in describing stage 1 are ordered.
That is, the first line whose condition is true applies.
A line without a condition is the default behavior when none of the earlier
lines apply.
For conversion from an integral type other than a character type, the
function determines the integral conversion specifier as indicated in
Table~\ref{tab:localization.integer.conversions.out}.
\begin{floattable}{Integer conversions}{tab:localization.integer.conversions.out}
{lc}
\topline
\lhdr{State} & \tcode{stdio} equivalent \\ \capsep
\tcode{basefield == ios_base::oct} & \tcode{\%o} \\ \rowsep
\tcode{(basefield == ios_base::hex) \&\& !uppercase} & \tcode{\%x} \\ \rowsep
\tcode{(basefield == ios_base::hex)} & \tcode{\%X} \\ \rowsep
for a \tcode{signed} integral type & \tcode{\%d} \\ \rowsep
for an \tcode{unsigned} integral type & \tcode{\%u} \\
\end{floattable}
For conversion from a floating-point type, the function determines
the floating-point conversion specifier as indicated in Table~\ref{tab:localization.fp.conversions.out}.
\begin{floattable}{Floating-point conversions}{tab:localization.fp.conversions.out}
{lc}
\topline
\lhdr{State} & \tcode{stdio} equivalent \\ \capsep
\tcode{floatfield == ios_base::fixed} & \tcode{\%f} \\ \rowsep
\tcode{floatfield == ios_base::scientific \&\& !uppercase} & \tcode{\%e} \\ \rowsep
\tcode{floatfield == ios_base::scientific} & \tcode{\%E} \\ \rowsep
\tcode{floatfield == (ios_base::fixed | ios_base::scientific) \&\& !uppercase} & \tcode{\%a} \\ \rowsep
\tcode{floatfield == (ios_base::fixed | ios_base::scientific)} & \tcode{\%A} \\ \rowsep
\tcode{!uppercase} & \tcode{\%g} \\ \rowsep
\textit{otherwise} & \tcode{\%G} \\
\end{floattable}
For conversions from an integral or floating-point
type a length modifier is added to the
conversion specifier as indicated in Table~\ref{tab:localization.length.modifier.out}.
\begin{floattable}{Length modifier}{tab:localization.length.modifier.out}
{lc}
\topline
\lhdr{Type} & Length modifier \\ \capsep
\tcode{long} & \tcode{l} \\ \rowsep
\tcode{long long} & \tcode{ll} \\ \rowsep
\tcode{unsigned long} & \tcode{l} \\ \rowsep
\tcode{unsigned long long} & \tcode{ll} \\ \rowsep
\tcode{long double} & \tcode{L} \\ \rowsep
\textit{otherwise} & \textit{none} \\
\end{floattable}
The conversion specifier has the following optional additional qualifiers
prepended as indicated in Table~\ref{tab:localization.numeric.conversions}.
\begin{floattable}{Numeric conversions}{tab:localization.numeric.conversions}
{llc}
\topline
\lhdr{Type(s)} & \chdr{State} & \tcode{stdio} equivalent \\ \capsep
an integral type & \tcode{flags \& showpos} & \tcode{+} \\
& \tcode{flags \& showbase} & \tcode{\#} \\ \rowsep
a floating-point type & \tcode{flags \& showpos} & \tcode{+} \\
& \tcode{flags \& showpoint} & \tcode{\#} \\
\end{floattable}
For conversion from a floating-point type,
if \tcode{floatfield != (ios_base::fixed | ios_base::\brk{}scientific)},
\tcode{str.precision()}
is specified as precision in the conversion specification.
Otherwise, no precision is specified.
For conversion from
\tcode{void*}
the specifier is
\tcode{\%p}.
The representations at the end of stage 1 consists of the
\tcode{char}'s
that would be printed by a call of
\tcode{printf(s, val)}
where \tcode{s} is the conversion specifier determined above.
\stage{2}
Any character \tcode{c} other than a decimal point(.) is converted to a
\tcode{charT}
via
\tcode{use_facet<ctype<charT> >(loc).widen( c )}
A local variable \tcode{punct} is initialized via
\begin{codeblock}
const numpunct<charT>& punct = use_facet< numpunct<charT> >(str.getloc());
\end{codeblock}
For arithmetic types,
\tcode{punct.thousands_sep()}
characters are inserted into the sequence as determined by the value returned
by
\tcode{punct.do_grouping()}
using the method described in~\ref{facet.numpunct.virtuals}
Decimal point characters(.) are replaced by
\tcode{punct.decimal_point()}
\stage{3}
A local variable is initialized as
\begin{codeblock}
fmtflags adjustfield= (flags & (ios_base::adjustfield));
\end{codeblock}
The location of any padding\footnote{The conversion specification
\tcode{\#o}
generates a leading
\tcode{0}
which is
\textit{not}
a padding character.} is determined according to Table~\ref{tab:localization.fill.padding}.
\begin{floattable}{Fill padding}{tab:localization.fill.padding}
{p{3in}l}
\topline
\lhdr{State} & \rhdr{Location} \\ \capsep
\tcode{adjustfield == ios_base::left} & pad after \\ \rowsep
\tcode{adjustfield == ios_base::right} & pad before \\ \rowsep
\tcode{adjustfield == internal} and a sign occurs in the representation
& pad after the sign \\ \rowsep
\tcode{adjustfield == internal} and representation after stage 1
began with 0x or 0X & pad after x or X \\ \rowsep
\textit{otherwise} & pad before \\
\end{floattable}
If
\tcode{str.width()}
is nonzero and the number of
\tcode{charT}'s
in the sequence after stage 2 is less than
\tcode{str.\brk{}width()},
then enough \tcode{fill} characters are added to the sequence at the position
indicated for padding to bring the length of the sequence to
\tcode{str.width()}.
\tcode{str.width(0)}
is called.
\stage{4}
The sequence of
\tcode{charT}'s
at the end of stage 3 are output via
\begin{codeblock}
*out++ = c
\end{codeblock}
\end{description}
\end{itemdescr}
\indexlibrary{\idxcode{do_put}!\idxcode{num_put}}%
\indexlibrary{\idxcode{num_put}!\idxcode{do_put}}%
\begin{itemdecl}
iter_type do_put(iter_type out, ios_base& str, char_type fill,
bool val) const;
\end{itemdecl}
\begin{itemdescr}
\pnum
\returns
If
\tcode{(str.flags() \& ios_base::boolalpha) == 0}
returns
\tcode{do_put(out, str, fill,\\(int)val)},
otherwise obtains a string
\tcode{s}
as if by
\begin{codeblock}
string_type s =
val ? use_facet<ctype<charT> >(loc).truename()
: use_facet<ctype<charT> >(loc).falsename();
\end{codeblock}
and then inserts each character
\tcode{c}
of
\tcode{s}
into
\tcode{out}
via
\tcode{*out++ = c}
and returns
\tcode{out}.
\end{itemdescr}
\rSec2[facet.numpunct]{The numeric punctuation facet}
\rSec3[locale.numpunct]{Class template \tcode{numpunct}}
\indexlibrary{\idxcode{numpunct}}%
\begin{codeblock}
namespace std {
template <class charT>
class numpunct : public locale::facet {
public:
typedef charT char_type;
typedef basic_string<charT> string_type;
explicit numpunct(size_t refs = 0);
char_type decimal_point() const;
char_type thousands_sep() const;
string grouping() const;
string_type truename() const;
string_type falsename() const;
static locale::id id;
protected:
~numpunct(); // virtual
virtual char_type do_decimal_point() const;
virtual char_type do_thousands_sep() const;
virtual string do_grouping() const;
virtual string_type do_truename() const; // for \tcode{bool}
virtual string_type do_falsename() const; // for \tcode{bool}
};
}
\end{codeblock}
\pnum
\tcode{numpunct<>}
specifies numeric punctuation.
The specializations required in Table~\ref{tab:localization.category.facets}~(\ref{locale.category}), namely
\tcode{numpunct<\brk{}wchar_t>}
and
\tcode{numpunct<char>},
provide classic
\tcode{"C"}
numeric formats,
i.e., they contain information equivalent to that contained in the
\tcode{"C"}
locale or their wide character counterparts as if obtained by
a call to
\tcode{widen}.
\pnum
The syntax for number formats is as follows, where
\tcode{digit}
represents the radix set specified by the
\tcode{fmtflags}
argument value, and
\tcode{thousands-sep}
and
\tcode{decimal-point}
are the results of corresponding
\tcode{numpunct<charT>}
members.
Integer values have the format:
\begin{codeblock}
integer ::= [sign] units
sign ::= plusminus
plusminus ::= '+' | '-'
units ::= digits [thousands-sep units]
digits ::= digit [digits]
\end{codeblock}
and floating-point values have:
\begin{codeblock}
floatval ::= [sign] units [decimal-point [digits]] [e [sign] digits] |
[sign] decimal-point digits [e [sign] digits]
e ::= 'e' | 'E'
\end{codeblock}
where the number of digits between
\tcode{thousands-sep}s
is as specified by
\tcode{do_grouping()}.
For parsing, if the
\tcode{digits}
portion contains no thousands-separators, no grouping constraint
is applied.
\rSec4[facet.numpunct.members]{\tcode{numpunct} members}
\indexlibrary{\idxcode{numpunct}!\idxcode{decimal_point}}%
\indexlibrary{\idxcode{decimal_point}!\idxcode{numpunct}}%
\begin{itemdecl}
char_type decimal_point() const;
\end{itemdecl}
\begin{itemdescr}
\pnum
\returns
\tcode{do_decimal_point()}
\end{itemdescr}
\indexlibrary{\idxcode{numpunct}!\idxcode{thousands_sep}}%
\indexlibrary{\idxcode{thousands_sep}!\idxcode{numpunct}}%
\begin{itemdecl}
char_type thousands_sep() const;
\end{itemdecl}
\begin{itemdescr}
\pnum
\returns
\tcode{do_thousands_sep()}
\end{itemdescr}
\indexlibrary{\idxcode{numpunct}!\idxcode{grouping}}%
\indexlibrary{\idxcode{grouping}!\idxcode{numpunct}}%
\begin{itemdecl}
string grouping() const;
\end{itemdecl}
\begin{itemdescr}
\pnum
\returns
\tcode{do_grouping()}
\end{itemdescr}
\indexlibrary{\idxcode{numpunct}!\idxcode{truename}}%
\indexlibrary{\idxcode{truename}!\idxcode{numpunct}}%
\indexlibrary{\idxcode{numpunct}!\idxcode{falsename}}%
\indexlibrary{\idxcode{falsename}!\idxcode{numpunct}}%
\begin{itemdecl}
string_type truename() const;
string_type falsename() const;
\end{itemdecl}
\begin{itemdescr}
\pnum
\returns
\tcode{do_truename()}
or
\tcode{do_falsename()},
respectively.
\end{itemdescr}
\rSec4[facet.numpunct.virtuals]{\tcode{numpunct} virtual functions}
\indexlibrary{\idxcode{numpunct}!\idxcode{do_decimal_point}}%
\indexlibrary{\idxcode{do_decimal_point}!\idxcode{numpunct}}%
\begin{itemdecl}
char_type do_decimal_point() const;
\end{itemdecl}
\begin{itemdescr}
\pnum
\returns
A character for use as the decimal radix separator.
The required specializations return \tcode{'.'} or \tcode{L'.'}.
\end{itemdescr}
\indexlibrary{\idxcode{numpunct}!\idxcode{do_thousands_sep}}%
\indexlibrary{\idxcode{do_thousands_sep}!\idxcode{numpunct}}%
\begin{itemdecl}
char_type do_thousands_sep() const;
\end{itemdecl}
\begin{itemdescr}
\pnum
\returns
A character for use as the digit group separator.
The required specializations return \tcode{','} or \tcode{L','}.
\end{itemdescr}
\indexlibrary{\idxcode{numpunct}!\idxcode{do_grouping}}%
\indexlibrary{\idxcode{do_grouping}!\idxcode{numpunct}}%
\begin{itemdecl}
string do_grouping() const;
\end{itemdecl}
\begin{itemdescr}
\pnum
\returns
A basic_string<char> \tcode{vec} used as a vector of integer values,
in which each element
\tcode{vec[i]}
represents the number of digits\footnote{Thus, the string
\tcode{"\textbackslash003"} specifies groups of 3 digits each, and
\tcode{"3"} probably indicates groups of 51 (!) digits each,
because 51 is the ASCII value of \tcode{"3"}.}
in the group at position \tcode{i}, starting with position 0 as the
rightmost group.
If
\tcode{vec.size() <= i},
the number is the same as group
\tcode{(i-1)};
if
\tcode{(i<0 || vec[i]<=0 || vec[i]==CHAR_MAX)},
the size of the digit group is unlimited.
\pnum
The required specializations return the empty string, indicating
no grouping.
\end{itemdescr}
\indexlibrary{\idxcode{numpunct}!\idxcode{do_truename}}%
\indexlibrary{\idxcode{do_truename}!\idxcode{numpunct}}%
\indexlibrary{\idxcode{numpunct}!\idxcode{do_falsename}}%
\indexlibrary{\idxcode{do_falsename}!\idxcode{numpunct}}%
\begin{itemdecl}
string_type do_truename() const;
string_type do_falsename() const;
\end{itemdecl}
\begin{itemdescr}
\pnum
\returns
A string representing the name of the boolean value
\tcode{true}
or
\tcode{false},
respectively.
\pnum
In the base class implementation these names are
\tcode{"true"} and \tcode{"false"}, or \tcode{L"true"} and \tcode{L"false"}.
\end{itemdescr}
\rSec3[locale.numpunct.byname]{Class template \tcode{numpunct_byname}}
\indexlibrary{\idxcode{numpunct_byname}}%
\begin{codeblock}
namespace std {
template <class charT>
class numpunct_byname : public numpunct<charT> {
// this class is specialized for \tcode{char} and \tcode{wchar_t}.
public:
typedef charT char_type;
typedef basic_string<charT> string_type;
explicit numpunct_byname(const char*, size_t refs = 0);
explicit numpunct_byname(const string&, size_t refs = 0);
protected:
~numpunct_byname();
};
}
\end{codeblock}
\rSec2[category.collate]{The collate category}
\rSec3[locale.collate]{Class template \tcode{collate}}
\indexlibrary{\idxcode{collate}}%
\begin{codeblock}
namespace std {
template <class charT>
class collate : public locale::facet {
public:
typedef charT char_type;
typedef basic_string<charT> string_type;
explicit collate(size_t refs = 0);
int compare(const charT* low1, const charT* high1,
const charT* low2, const charT* high2) const;
string_type transform(const charT* low, const charT* high) const;
long hash(const charT* low, const charT* high) const;
static locale::id id;
protected:
~collate();
virtual int do_compare(const charT* low1, const charT* high1,
const charT* low2, const charT* high2) const;
virtual string_type do_transform(const charT* low, const charT* high) const;
virtual long do_hash (const charT* low, const charT* high) const;
};
}
\end{codeblock}
\pnum
The class
\tcode{collate<charT>}
provides features for use in the
collation (comparison) and hashing of strings.
A locale member function template,
\tcode{operator()},
uses the collate facet to allow a locale to act directly as the predicate
argument for standard algorithms (Clause~\ref{algorithms}) and containers operating on strings.
The specializations required in Table~\ref{tab:localization.category.facets}~(\ref{locale.category}), namely
\tcode{collate<char>}
and
\tcode{collate<wchar_t>},
apply lexicographic ordering~(\ref{alg.lex.comparison}).
\pnum
Each function compares a string of characters
\tcode{*p}
in the range
\range{low}{high}.
\rSec4[locale.collate.members]{\tcode{collate} members}
\indexlibrary{\idxcode{collate}!\idxcode{compare}}%
\indexlibrary{\idxcode{compare}!\idxcode{collate}}%
\begin{itemdecl}
int compare(const charT* low1, const charT* high1,
const charT* low2, const charT* high2) const;
\end{itemdecl}
\begin{itemdescr}
\pnum
\returns
\tcode{do_compare(low1, high1, low2, high2)}
\end{itemdescr}
\indexlibrary{\idxcode{collate}!\idxcode{transform}}%
\indexlibrary{\idxcode{transform}!\idxcode{collate}}%
\begin{itemdecl}
string_type transform(const charT* low, const charT* high) const;
\end{itemdecl}
\begin{itemdescr}
\pnum
\returns
\tcode{do_transform(low, high)}
\end{itemdescr}
\indexlibrary{\idxcode{collate}!\idxcode{hash}}%
\indexlibrary{\idxcode{hash}!\idxcode{collate}}%
\begin{itemdecl}
long hash(const charT* low, const charT* high) const;
\end{itemdecl}
\begin{itemdescr}
\pnum
\returns
\tcode{do_hash(low, high)}
\end{itemdescr}
\rSec4[locale.collate.virtuals]{\tcode{collate} virtual functions}
\indexlibrary{\idxcode{collate}!\idxcode{do_compare}}%
\indexlibrary{\idxcode{do_compare}!\idxcode{collate}}%
\begin{itemdecl}
int do_compare(const charT* low1, const charT* high1,
const charT* low2, const charT* high2) const;
\end{itemdecl}
\begin{itemdescr}
\pnum
\returns
\tcode{1}
if the first string is greater than the second,
\tcode{-1}
if less, zero otherwise.
The specializations required in Table~\ref{tab:localization.category.facets}~(\ref{locale.category}), namely
\tcode{collate<char>}
and
\tcode{collate<wchar_t>},
implement
a lexicographical comparison~(\ref{alg.lex.comparison}).
\end{itemdescr}
\indexlibrary{\idxcode{collate}!\idxcode{do_transform}}%
\indexlibrary{\idxcode{do_transform}!\idxcode{collate}}%
\begin{itemdecl}
string_type do_transform(const charT* low, const charT* high) const;
\end{itemdecl}
\begin{itemdescr}
\pnum
\returns
A
\tcode{basic_string<charT>}
value that, compared lexicographically with the result of calling
\tcode{transform()}
on another string, yields the same result as calling
\tcode{do_compare()}
on the same two strings.\footnote{This function is useful when one string is
being compared to many other strings.}
\end{itemdescr}
\indexlibrary{\idxcode{collate}!\idxcode{do_hash}}%
\indexlibrary{\idxcode{do_hash}!\idxcode{collate}}%
\begin{itemdecl}
long do_hash(const charT* low, const charT* high) const;
\end{itemdecl}
\begin{itemdescr}
\pnum
\returns
An integer value equal to the result of calling
\tcode{hash()}
on any other string for which
\tcode{do_compare()}
returns 0 (equal) when passed the two strings.
\enternote
The probability that the result equals that for another string which does
not compare equal should be very small, approaching
\tcode{(1.0/numeric_limits<unsigned long>::max())}.
\exitnote
\end{itemdescr}
\rSec3[locale.collate.byname]{Class template \tcode{collate_byname}}
\indexlibrary{\idxcode{collate_byname}}%
\begin{codeblock}
namespace std {
template <class charT>
class collate_byname : public collate<charT> {
public:
typedef basic_string<charT> string_type;
explicit collate_byname(const char*, size_t refs = 0);
explicit collate_byname(const string&, size_t refs = 0);
protected:
~collate_byname();
};
}
\end{codeblock}
\rSec2[category.time]{The time category}
\pnum
Templates
\tcode{time_get<charT,InputIterator>}
and
\tcode{time_put<charT,OutputIterator>}
provide date and time formatting and parsing.
All specifications of member functions for
\tcode{time_put}
and
\tcode{time_get}
in the subclauses of~\ref{category.time} only apply to the
specializations required in Tables~\ref{tab:localization.category.facets}
and~\ref{tab:localization.required.specializations}~(\ref{locale.category}).
Their members use their
\tcode{ios_base\&},
\tcode{ios_base::iostate\&},
and
\tcode{fill}
arguments as described in~(\ref{locale.categories}), and the
\tcode{ctype<>}
facet, to determine formatting details.
\rSec3[locale.time.get]{Class template \tcode{time_get}}
\indexlibrary{\idxcode{time_get}}%
\begin{codeblock}
namespace std {
class time_base {
public:
enum dateorder { no_order, dmy, mdy, ymd, ydm };
};
template <class charT, class InputIterator = istreambuf_iterator<charT> >
class time_get : public locale::facet, public time_base {
public:
typedef charT char_type;
typedef InputIterator iter_type;
explicit time_get(size_t refs = 0);
dateorder date_order() const { return do_date_order(); }
iter_type get_time(iter_type s, iter_type end, ios_base& f,
ios_base::iostate& err, tm* t) const;
iter_type get_date(iter_type s, iter_type end, ios_base& f,
ios_base::iostate& err, tm* t) const;
iter_type get_weekday(iter_type s, iter_type end, ios_base& f,
ios_base::iostate& err, tm* t) const;
iter_type get_monthname(iter_type s, iter_type end, ios_base& f,
ios_base::iostate& err, tm* t) const;
iter_type get_year(iter_type s, iter_type end, ios_base& f,
ios_base::iostate& err, tm* t) const;
iter_type get(iter_type s, iter_type end, ios_base& f,
ios_base::iostate& err, tm *t, char format, char modifier = 0) const;
iter_type get(iter_type s, iter_type end, ios_base& f,
ios_base::iostate& err, tm *t, const char_type *fmt,
const char_type *fmtend) const;
static locale::id id;
protected:
~time_get();
virtual dateorder do_date_order() const;
virtual iter_type do_get_time(iter_type s, iter_type end, ios_base&,
ios_base::iostate& err, tm* t) const;
virtual iter_type do_get_date(iter_type s, iter_type end, ios_base&,
ios_base::iostate& err, tm* t) const;
virtual iter_type do_get_weekday(iter_type s, iter_type end, ios_base&,
ios_base::iostate& err, tm* t) const;
virtual iter_type do_get_monthname(iter_type s, iter_type end, ios_base&,
ios_base::iostate& err, tm* t) const;
virtual iter_type do_get_year(iter_type s, iter_type end, ios_base&,
ios_base::iostate& err, tm* t) const;
virtual iter_type do_get(iter_type s, iter_type end, ios_base& f,
ios_base::iostate& err, tm *t, char format, char modifier) const;
};
}
\end{codeblock}
\pnum
\tcode{time_get}
is used to
parse a character sequence, extracting components of a time or date
into a
\tcode{struct tm}
record.
Each
\tcode{get}
member parses a format as produced by a corresponding format specifier to
\tcode{time_put<>::put}.
If the sequence being parsed matches the correct format, the corresponding
members of the
\tcode{struct tm}
argument are set to the values used to produce the sequence; otherwise
either an error is reported or unspecified values are assigned.\footnote{In
other words, user confirmation is required for reliable parsing of
user-entered dates and times, but machine-generated formats can be
parsed reliably.
This allows parsers to be aggressive about
interpreting user variations on standard formats.}
\pnum
If the end iterator is reached during parsing by any of the
\tcode{get()}
member functions, the member sets
\tcode{ios_base::eof\-bit}
in \tcode{err}.
\rSec4[locale.time.get.members]{\tcode{time_get} members}
\indexlibrary{\idxcode{time_get}!\idxcode{date_order}}%
\indexlibrary{\idxcode{date_order}!\idxcode{time_get}}%
\begin{itemdecl}
dateorder date_order() const;
\end{itemdecl}
\begin{itemdescr}
\pnum
\returns
\tcode{do_date_order()}