std::num_get::get
std::num_get::get, std::num_get::do_get
| (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; | |
1) Public member function, calls the member function do_get
of the most derived class.
2) Reads characters from the input iterator 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.
Stage 1: conversion specifier selection
- I/O format flags are obtained, as if by
fmtflags basefield =(str.flags()&
std::ios_base::basefield
fmtflags boolalpha =(str.flags()&st
d::ios_base::boolalpha
- If the type of
v
is an integer type, the the first applicable choice of the following fiv
e is selected:
If basefield == oct
, will use conv
ersion specifier %o
If basefield == hex
, will use conv
ersion specifier %X
If basefield == 0
, will use conv
ersion specifier %i
If the type of v
is signed, will use conv
ersion specifier %d
If the type of v
is unsigned, will use conv
ersion specifier %u
- For integer types,
l
ength
modifier is added to th
e conversion specification if necessary:h
forshort
andunsigned short
,l
forlong
andunsigned long
,ll
forlong long
andunsigned long long
Stage 2: character extraction
- If
in==end
, Stage 2 is terminated immediately, no further characters are extracted
Stage 3: conversion and storage
- The sequence of
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) |
If the conversion function fails to convert the entire field, the value 0 is stored in v If the conversion function results in a positive value too large to fit in the type of v, the most positive representable value is stored in v If the conversion function results in a negative value too large to fit in the type of v, the most negative representable value is stored in v, or zero for unsigned integer types. (until C++17) | (since C++11) |
---|
- If the con
v
ersion function fails to conv
ert the entire field, thev
alue0
is stored inv
(since C++11)
- In any case, if the conversion function fails
std::ios_base::failbit
is assigned toerr
Return value
in
.
Notes
In C++98/C++03, if an error occurs, v
is left unchanged. In C++11, it is set to a v
alue as described abov
e.
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 LLONG_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.
Example
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::istreambuf_iterator<CharT, Traits> end;
std::use_facet<std::num_get<CharT>>(is.getloc()).get(is, end, is, err, b.x
}
} catch(std::ios_base::failure& error)
{
// handle the exception
}
return is;
}
int main()
{
base b;
std::cin >> b;
}
See also
operator>> | extracts formatted data (public member function of std::basic_istream) |
---|
© cppreference.com
Licensed under the Creative Commons Attribution-ShareAlike Unported License v3.0.