std::basic_istream<CharT,Traits>::operator>>

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

# 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

DRApplied toBehavior as publishedCorrect behavior
LWG 64C++98it was unclear whether overload (18) can only rethrow thestd::ios_base::failure thrown by calling setstate(failbit)all exceptions caughtcan be rethrown
LWG 118C++98overload (12,13) delegated the extraction to num_get::get,but it does not have overloads for short and inta long value is extractedinstead of short or int
LWG 413C++98overload (18) only rethrew exceptions thrown while extractingcharacters from sb, but characters are extracted from *thiscorrected sb to *this
LWG 567C++98overload (18) behaved as a FormattedInputFunctionbecause of the resolution of LWG issue 60it behaves as anUnformattedInputFunction
LWG 661C++98overloads (12,13) did not store the extracted numberto value due to the resolution of LWG issue 118stores the number ifno overflow occurs
LWG 696C++98value was unchanged on extraction failureset to zero or minimum/maximum values

# See also