[category.numeric] (original) (raw)

28 Text processing library [text]

28.3 Localization library [localization]

28.3.4 Standard locale categories [locale.categories]

28.3.4.3 The numeric category [category.numeric]


28.3.4.3.1 General [category.numeric.general]

28.3.4.3.2 Class template num_get [locale.num.get]

28.3.4.3.2.1 General [locale.num.get.general]

28.3.4.3.2.2 Members [facet.num.get.members]

28.3.4.3.2.3 Virtual functions [facet.num.get.virtuals]

28.3.4.3.3 Class template num_put [locale.nm.put]

28.3.4.3.3.1 General [locale.nm.put.general]

28.3.4.3.3.2 Members [facet.num.put.members]

28.3.4.3.3.3 Virtual functions [facet.num.put.virtuals]


28.3.4.3.1 General [category.numeric.general]

The classes num_get<> and 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.[223](#footnote-223 "Parsing "-1" correctly into, e.g., an unsigned short requires that the corresponding member get() at least extract the sign before delegating.")

All specifications of member functions for num_put and num_getin the subclauses of [category.numeric] only apply to the specializations required in Tables 91and 92 ([locale.category]), namelynum_get<char>,num_get<wchar_t>,num_get<C, InputIterator>,num_put<char>,num_put<wchar_t>, andnum_put<C, OutputIterator>.

These specializations refer to the ios_base& argument for formatting specifications ([locale.categories]), and to its imbued locale for the numpunct<> facet to identify all numeric punctuation preferences, and also for the ctype<> facet to perform character classification.

28.3.4.3.2 Class template num_get [locale.num.get]

28.3.4.3.2.1 General [locale.num.get.general]

namespace std { template<class charT, class InputIterator = istreambuf_iterator<charT>> class num_get : public locale::facet { public: using char_type = charT;using iter_type = InputIterator;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;};}

The facet num_get is used to parse numeric values from an input sequence such as an istream.

28.3.4.3.2.2 Members [facet.num.get.members]

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;

Returns: do_get(in, end, str, err, val).

28.3.4.3.2.3 Virtual functions [facet.num.get.virtuals]

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;

Effects: Reads characters from in, interpreting them according tostr.flags(),use_facet<ctype<​charT>>(loc), anduse_facet<numpunct<charT>>(loc), where loc is str.getloc().

The details of this operation occur in three stages:

The details of the stages are presented below.

Digit grouping is checked.

That is, the positions of discarded separators are examined for consistency withuse_facet<numpunct<charT>>(loc).grouping().

If they are not consistent then ios_base​::​failbit is assigned to err.

In any case, if stage 2 processing was terminated by the test for in == endthen err |= ios_base​::​eofbit is performed.

iter_type do_get(iter_type in, iter_type end, ios_base& str, ios_base::iostate& err, bool& val) const;

Effects: If (str.flags() & ios_base​::​boolalpha) == 0then input proceeds as it would for a longexcept that if a value is being stored into val, the value is determined according to the following: If the value to be stored is 0 then false is stored.

If the value is 1 then true is stored.

Otherwise true is stored andios_base​::​failbit is assigned to err.

Otherwise target sequences are determined “as if” by calling the members falsename() and truename() of the facet obtained by use_facet<numpunct<charT>>(str.getloc()).

Successive characters in the range [in, end) (see [sequence.reqmts]) are obtained and matched against corresponding positions in the target sequences only as necessary to identify a unique match.

The input iterator in is compared to endonly when necessary to obtain a character.

If a target sequence is uniquely matched,val is set to the corresponding value.

Otherwise false is stored andios_base​::​failbit is assigned to err.

The in iterator is always left pointing one position beyond the last character successfully matched.

If val is set, then err is set to str.goodbit; or to str.eofbit if, when seeking another character to match, it is found that (in == end).

If val is not set, then err is set to str.failbit; or to (str.failbit | str.eofbit)if the reason for the failure was that (in == end).

[Example 2:

For targets true: "a" and false: "abb", the input sequence "a" yieldsval == true and err == str.eofbit; the input sequence "abc" yieldserr = str.failbit, with in ending at the 'c' element.

For targets true: "1" and false: "0", the input sequence "1" yieldsval == true and err == str.goodbit.

For empty targets (""), any input sequence yields err == str.failbit.

— _end example_]

28.3.4.3.3 Class template num_put [locale.nm.put]

28.3.4.3.3.1 General [locale.nm.put.general]

namespace std { template<class charT, class OutputIterator = ostreambuf_iterator<charT>> class num_put : public locale::facet { public: using char_type = charT;using iter_type = OutputIterator;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;};}

The facetnum_putis used to format numeric values to a character sequence such as an ostream.

28.3.4.3.3.2 Members [facet.num.put.members]

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;

Returns: do_put(out, str, fill, val).

28.3.4.3.3.3 Virtual functions [facet.num.put.virtuals]

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;

Effects: Writes characters to the sequence out, formatting val as desired.

In the following description, loc names a local variable initialized aslocale loc = str.getloc();

The details of this operation occur in several stages:

Detailed descriptions of each stage follow.

iter_type do_put(iter_type out, ios_base& str, char_type fill, bool val) const;

Returns: If (str.flags() & ios_base​::​boolalpha) == 0returns do_put(out, str, fill,
(int)val), otherwise obtains a string s as if bystring_type s = val ? use_facet<numpunct<charT>>(loc).truename() : use_facet<numpunct<charT>>(loc).falsename();and then inserts each character c of s into outvia *out++ = cand returns out.