std::basic_istream<CharT,Traits>::operator>>
Min standard notice:
Extracts values from an input stream.
# Declarations
basic_istream& operator>>( unsigned short& value );
basic_istream& operator>>( unsigned int& value );
basic_istream& operator>>( long& value );
basic_istream& operator>>( unsigned long& value );
basic_istream& operator>>( long long& value );
(since C++11)
basic_istream& operator>>( unsigned long long& value );
(since C++11)
basic_istream& operator>>( float& value );
basic_istream& operator>>( double& value );
basic_istream& operator>>( long double& value );
basic_istream& operator>>( bool& value );
basic_istream& operator>>( void*& value );
basic_istream& operator>>( short& value );
basic_istream& operator>>( int& value );
basic_istream& operator>>( /* extended-floating-point-type */& value );
(since C++23)
basic_istream& operator>>( std::ios_base& (*func)(std::ios_base&) );
basic_istream& operator>>( std::basic_ios<CharT, Traits>&
(*func)(std::basic_ios<CharT, Traits>&) );
basic_istream& operator>>( basic_istream& (*func)(basic_istream&) );
basic_istream& operator>>( std::basic_streambuf<CharT, Traits>* sb );
# Parameters
value: reference to an integer or floating-point value to store the extracted value tofunc: pointer to I/O manipulator functionsb: pointer to the stream buffer to write all the data to
# Notes
For overload (14), when the extended floating-point type has a floating-point conversion rank that is not equal to the rank of any standard floating-point type, then double rounding during the conversion can result in inaccurate results. std::from_chars() can be used in situations where maximum accuracy is important.
# Example
#include <iomanip>
#include <iostream>
#include <sstream>
int main()
{
std::string input = "41 3.14 false hello world";
std::istringstream stream(input);
int n;
double f;
bool b;
stream >> n >> f >> std::boolalpha >> b;
std::cout << "n = " << n << '\n'
<< "f = " << f << '\n'
<< "b = " << std::boolalpha << b << '\n';
// extract the rest using the streambuf overload
stream >> std::cout.rdbuf();
std::cout << '\n';
}
# Defect reports
| DR | Applied to | Behavior as published | Correct behavior |
|---|---|---|---|
| LWG 64 | C++98 | it was unclear whether overload (18) can only rethrow thestd::ios_base::failure thrown by calling setstate(failbit) | all exceptions caughtcan be rethrown |
| LWG 118 | C++98 | overload (12,13) delegated the extraction to num_get::get,but it does not have overloads for short and int | a long value is extractedinstead of short or int |
| LWG 413 | C++98 | overload (18) only rethrew exceptions thrown while extractingcharacters from sb, but characters are extracted from *this | corrected sb to *this |
| LWG 567 | C++98 | overload (18) behaved as a FormattedInputFunctionbecause of the resolution of LWG issue 60 | it behaves as anUnformattedInputFunction |
| LWG 661 | C++98 | overloads (12,13) did not store the extracted numberto value due to the resolution of LWG issue 118 | stores the number ifno overflow occurs |
| LWG 696 | C++98 | value was unchanged on extraction failure | set to zero or minimum/maximum values |