(1) | ||
public: iter_type get( iter_type in, iter_type end, std::ios_base& str, std::ios_base::iostate& err, bool& v ) const; | ||
iter_type get( iter_type in, iter_type end, std::ios_base& str, std::ios_base::iostate& err, long& v ) const; | ||
iter_type get( iter_type in, iter_type end, std::ios_base& str, std::ios_base::iostate& err, long long& v ) const; | ||
iter_type get( iter_type in, iter_type end, std::ios_base& str, std::ios_base::iostate& err, unsigned short& v ) const; | ||
iter_type get( iter_type in, iter_type end, std::ios_base& str, std::ios_base::iostate& err, unsigned int& v ) const; | ||
iter_type get( iter_type in, iter_type end, std::ios_base& str, std::ios_base::iostate& err, unsigned long& v ) const; | ||
iter_type get( iter_type in, iter_type end, std::ios_base& str, std::ios_base::iostate& err, unsigned long long& v ) const; | ||
iter_type get( iter_type in, iter_type end, std::ios_base& str, std::ios_base::iostate& err, float& v ) const; | ||
iter_type get( iter_type in, iter_type end, std::ios_base& str, std::ios_base::iostate& err, double& v ) const; | ||
iter_type get( iter_type in, iter_type end, std::ios_base& str, std::ios_base::iostate& err, long double& v ) const; | ||
iter_type get( iter_type in, iter_type end, std::ios_base& str, std::ios_base::iostate& err, void*& v ) const; | ||
(2) | ||
protected: virtual iter_type do_get( iter_type in, iter_type end, std::ios_base& str, std::ios_base::iostate& err, bool& v ) const; | ||
virtual iter_type do_get( iter_type in, iter_type end, std::ios_base& str, std::ios_base::iostate& err, long& v ) const; | ||
virtual iter_type do_get( iter_type in, iter_type end, std::ios_base& str, std::ios_base::iostate& err, long long& v ) const; | ||
virtual iter_type do_get( iter_type in, iter_type end, std::ios_base& str, std::ios_base::iostate& err, unsigned short& v ) const; | ||
virtual iter_type do_get( iter_type in, iter_type end, std::ios_base& str, std::ios_base::iostate& err, unsigned int& v ) const; | ||
virtual iter_type do_get( iter_type in, iter_type end, std::ios_base& str, std::ios_base::iostate& err, unsigned long& v ) const; | ||
virtual iter_type do_get( iter_type in, iter_type end, std::ios_base& str, std::ios_base::iostate& err, unsigned long long& v ) const; | ||
virtual iter_type do_get( iter_type in, iter_type end, std::ios_base& str, std::ios_base::iostate& err, float& v ) const; | ||
virtual iter_type do_get( iter_type in, iter_type end, std::ios_base& str, std::ios_base::iostate& err, double& v ) const; | ||
virtual iter_type do_get( iter_type in, iter_type end, std::ios_base& str, std::ios_base::iostate& err, long double& v ) const; | ||
virtual iter_type do_get( iter_type in, iter_type end, std::ios_base& str, std::ios_base::iostate& err, void*& v ) const; |
do_get
of the most derived class.in
and generates the value of the type of v
, taking into account IO stream formatting flags from str.flags()
, character classification rules from std::use_facet<std::ctype<charT>>(str.getloc())
, and numeric punctuation characters from std::use_facet<std::numpunct<charT>>(str.getloc())
. This function is called by all formatted input stream operators such as std::cin >> n;
.Conversion occurs in three stages.
fmtflags basefield = (str.flags() & std::ios_base::basefield);
fmtflags boolalpha = (str.flags() & std::ios_base::boolalpha);
v
is an integer type, the the first applicable choice of the following five is selected: basefield == oct
, will use conversion specifier %o
basefield == hex
, will use conversion specifier %X
basefield == 0
, will use conversion specifier %i
v
is signed, will use conversion specifier %d
v
is unsigned, will use conversion specifier %u
h
for short
and unsigned short
, l
for long
and unsigned long
, ll
for long long
and unsigned long long
v
is float
, will use conversion specifier %g
v
is double
, will use conversion specifier %lg
v
is long double
, will use conversion specifier %Lg
v
is void*
, will use conversion specifier %p
v
is bool
and boolalpha==0
, proceeds as if the type of v
is long
, except for the value to be stored in v
in stage 3. v
is bool
and boolalpha!=0
, the following replaces stages 2 and 3: in
are matched against the character sequences obtained from std::use_facet<std::numpunct<charT>>(str.getloc()).falsename()
and std::use_facet<std::numpunct<charT> >(str.getloc()).truename()
only as necessary as to identify the unique match. The input iterator in
is compared to end
only when necessary to obtain a character. v
is set to the corresponding bool
value. Otherwise false
is stored in v
and std::ios_base::failbit
is assigned to err
. If unique match could not be found before the input ended (in==end
), err|=std::ios_base::eofbit
is executed. in==end
, Stage 2 is terminated immediately, no further characters are extracted in
as if by char_type ct = *in;
"0123456789abcdefxABCDEFX+-"
, widened to the locale's char_type as if by std::use_facet<std::ctype<charT>>(str.getloc()).widen()
, it is converted to the corresponding char
. std::use_facet<std::numpunct<charT>>(str.getloc()).decimal_point())
), it is replaced by '.'
. std::use_facet<std::numpunct<charT>>(str.getloc()).thousands_sep()
) and the thousands separation is in use at all std::use_facet<std::numpunct<charT>>(str.getloc()).grouping().length() != 0
, then if the decimal point '.'
has not yet been accumulated, the position of the character is remembered, but the character is otherwise ignored. If the decimal point has already been accumulated, the character is discarded and Stage 2 terminates. char
obtained from the previous steps is allowed in the input field that would be parsed by std::scanf
given the conversion specifier selected in Stage 1. If it is allowed, it is accumulated in a temporary buffer and Stage 2 repeats. If it is not allowed, Stage 2 terminates. char
s accumulated in Stage 2 is converted to a numeric value The input is parsed as if by std::scanf with the conversion specifier selected in Stage 1 | (until C++11) |
The input is parsed as if by std::strtoll for signed integer v , std::strtoull for unsigned integer v , or std::strtold for floating-point v |
(since C++11) (until C++17) |
The input is parsed as if by std::strtoll for signed integer v , std::strtoull for unsigned integer v , std::strtof for float v , std::strtod for double v , or std::strtold for long double v | (since C++17) |
| (since C++11) |
std::ios_base::failbit
is assigned to err
v
v
is bool
and boolalpha is not set, then if the value to be stored is 0
, false
is stored, if the value to be stored is 1
, true
is stored, for any other value std::ios_base::failbit
is assigned to err
and true
is stored. std::use_facet<std::numpunct<charT>>(str.getloc()).grouping()
, std::ios_base::failbit
is assigned to err
. in==end
, err|=std::ios_base::eofbit
is executed to set the eof bit. in
.
In C++98/C++03, if an error occurs, v
is left unchanged. In C++11, it is set to a value as described above.
The result of converting a negative number string into an unsigned integer was specified to produce zero until C++17, although some implementations followed the protocol of std::strtoull
which negates in the target type, giving ULLONG_MAX
for "-1"
, and so produce the largest value of the target type instead. As of C++17, strictly following std::strtoull
is the correct behavior.
Because stage 2 filters out characters such as 'p', 'N' or 'i', the hexadecimal floating-point numbers such as "0x1.23p-10" and the strings "NaN" or "inf" may be rejected by do_get(double)
even if they are valid input to strtod
: this is LWG #2381.
An implementation of operator>>
for a user-defined type.
#include <iostream> #include <iterator> #include <locale> struct base { long x; }; template <class CharT, class Traits> std::basic_istream<CharT, Traits>& operator >>(std::basic_istream<CharT, Traits>& is, base& b) { std::ios_base::iostate err = std::ios_base::goodbit; try // setting err could throw { typename std::basic_istream<CharT, Traits>::sentry s(is); if (s) // if stream is ready for input { std::use_facet<std::num_get<CharT>>(is.getloc()).get(is, {}, is, err, b.x); } } catch(std::ios_base::failure& error) { // handle the exception } return is; } int main() { base b; std::cin >> b; }
extracts formatted data (public member function of std::basic_istream ) |
© cppreference.com
Licensed under the Creative Commons Attribution-ShareAlike Unported License v3.0.
http://en.cppreference.com/w/cpp/locale/num_get/get