std::num_get<CharT,InputIt>::get, std::num_get<CharT,InputIt>::do_get

  1. Public member function, calls the member function do_get of the most derived class.

# Declarations

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;

(since C++11)

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;

(since C++11)

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;
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;

(since C++11)

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;

(since C++11)

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;

# Return value

in

# Notes

Before the resolutions of LWG issue 23 and LWG issue 696, v was left unchanged if an error occurs.

Before the resolution of LWG issue 221, strings representing hexadecimal integers (e.g. “0xA0”) were rejected by do_get(int) even if they are valid input to strtol because stage 2 filters out characters ‘X’ and ‘x’.

Before the resolution of LWG issue 1169, converting a negative number string into an unsigned integer might produce zero (since the value represented by the string is smaller than what the target type can represent).

Before the resolution of LWG issue 2381, strings representing hexadecimal floating-point numbers with exponents (e.g. “0x1.23p-10”) were rejected by do_get(double) even if they are valid input to strtod because stage 2 filters out characters ‘P’ and ‘p’.

The strings representing infinity or not-a-number (e.g. “NaN” and “inf”) are rejected by do_get(double) even if they are valid input to strtod because stage 2 filters out characters such as ‘N’ or ‘i’.

# Example

#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;
}

# Defect reports

DRApplied toBehavior as publishedCorrect behavior
LWG 17C++98the process of parsing text boolean values was errornouscorrected
LWG 18C++98the overload of get taking bool& value was missingadded
LWG 23C++98overflowing input resulted in undefined behavioroverflow handled
LWG 154C++98the conversion specifier for double was %g (same as float)changed to %lg
LWG 221C++98do_get did not parse ‘x’ and ‘X’ while strtol parsed themmade ‘x’ and ‘X’ parsed
LWG 275C++98get had an overload taking short& value instead of float&corrected
LWG 358C++98thousand separators after the decimal point were ignoredstage 2 is terminated if encountered
LWG 696C++98the result was unchanged on conversion failureset to zero
LWG 1169C++98overflow handling was inconsistent between floating-point typesmade consistentwith strtof/strtod
LWG 2381C++11do_get did not parse ‘p’ and ‘P’ while strtod parsed themmade ‘p’ and ‘P’ parsed

# See also