std::basic_...:operator>>(3)
πΈ NAME πΈ
okay so this is the std::basic_istream::operator>> page from the C++ Standard Library manual, section 3, and it's literally the extraction operator β the >> bestie you use to pull values out of an input stream. The official name is std::basic_istream::operator>>, and yes it shows up twice in the NAME line because the manual is dramatic like that. π
β¨ SYNOPSIS β¨
vibe check: here's the whole overload lineup, no cap:
basic_istream& operator>>( unsigned short& value ); // (1)
basic_istream& operator>>( unsigned int& value ); // (2)
basic_istream& operator>>( long& value ); // (3)
basic_istream& operator>>( unsigned long& value ); // (4)
basic_istream& operator>>( long long& value ); // (5) (since C++11)
basic_istream& operator>>( unsigned long long& value ); // (6) (since C++11)
basic_istream& operator>>( float& value ); // (7)
basic_istream& operator>>( double& value ); // (8)
basic_istream& operator>>( long double& value ); // (9)
basic_istream& operator>>( bool& value ); // (10)
basic_istream& operator>>( void*& value ); // (11)
basic_istream& operator>>( short& value ); // (12)
basic_istream& operator>>( int& value ); // (13)
basic_istream& operator>>( /* extended-floating-point-type */& value ); // (14) (since C++23)
basic_istream& operator>>( std::ios_base& (*func)(std::ios_base&) ); // (15)
basic_istream& operator>>( std::basic_ios<CharT, Traits>& (*func)(std::basic_ios<CharT, Traits>&) ); // (16)
basic_istream& operator>>( basic_istream& (*func)(basic_istream&) ); // (17)
basic_istream& operator>>( std::basic_streambuf<CharT, Traits>* sb ); // (18)
π DESCRIPTION π
The one-line summary is: extracts values from an input stream. That's the main character energy right there. Now here's the overload tea, fr fr:
- Overloads (1)β(11) β extracts a value, potentially skipping preceding whitespace. The value is stored to a given reference
value. This function behaves as aFormattedInputFunction. After constructing and checking thesentryobject, which may skip leading whitespace, it extracts a value by callingstd::num_get::get(). π«Ά - Overload (12) β extracts a
shortvalue, potentially skipping preceding whitespace. The value is stored to a given referencevalue. This function behaves as aFormattedInputFunction. After constructing and checking thesentryobject, which may skip leading whitespace, it extracts alongvaluelvalby callingstd::num_get::get(). After that:- If
lval < std::numeric_limits<short>::min(), setsfailbitand storesstd::numeric_limits<short>::min()toval. - Otherwise, if
std::numeric_limits<short>::max() < lval, setsfailbitand storesstd::numeric_limits<short>::max()toval. - Otherwise, stores
static_cast<short>(lval)toval.
- If
- Overload (13) β extracts an
intvalue, potentially skipping preceding whitespace. The value is stored to a given referencevalue. This function behaves as aFormattedInputFunction. After constructing and checking thesentryobject, which may skip leading whitespace, it extracts alongvaluelvalby callingstd::num_get::get(). After that:- If
lval < std::numeric_limits<int>::min(), setsfailbitand storesstd::numeric_limits<int>::min()toval. - Otherwise, if
std::numeric_limits<int>::max() < lval, setsfailbitand storesstd::numeric_limits<int>::max()toval. - Otherwise, stores
static_cast<int>(lval)toval.
- If
- Overload (14) β extracts an extended floating-point value, potentially skipping preceding whitespace. The value is stored to a given reference
value. The library provides overloads for all cv-unqualified extended floating-point types as the referenced type of the parametervalue.- Determines the standard floating-point type
FPas follows:- If the floating-point conversion rank of
/* extended-floating-point-type */is less than or equal to that offloat, thenFPisfloat. - Otherwise, if the floating-point conversion rank of
/* extended-floating-point-type */is less than or equal to that ofdouble, thenFPisdouble. - Otherwise,
FPislong double.
- If the floating-point conversion rank of
- This function behaves as a
FormattedInputFunction. After constructing and checking thesentryobject, which may skip leading whitespace, it extracts anFPvaluefvalby callingstd::num_get::get(). After that:- If
fval < -std::numeric_limits</* extended-floating-point-type */>::max(), setsfailbitand stores-std::numeric_limits</* extended-floating-point-type */>::max()toval. - Otherwise, if
std::numeric_limits</* extended-floating-point-type */>::max() < fval, setsfailbitand storesstd::numeric_limits</* extended-floating-point-type */>::max()toval. - Otherwise, stores
static_cast</* extended-floating-point-type */>(fval)toval.
- If
- Determines the standard floating-point type
- Overloads (15)β(17) β calls
func(*this), wherefuncis an I/O manipulator. That's the whole thing. Slay. β¨ - Overload (18) β behaves as an
UnformattedInputFunction. After constructing and checking thesentryobject, it extracts all data from*thisand stores it tosb. The extraction stops if one of the following conditions are met:- end-of-file occurs on the input sequence;
- inserting in the output sequence fails (in which case the character to be inserted is not extracted);
- an exception occurs (in which case the exception is caught, and only rethrown if it inserted no characters and
failbitis enabled inexceptions()).
In either case, it stores the number of characters extracted in the member variable accessed by subsequent calls to gcount(). If sb is a null pointer or if no characters were inserted into sb, calls setstate(failbit) (which may throw std::ios_base::failure if enabled).
β οΈ extraction-fail tea, bestie: if extraction fails (e.g. if a letter was entered where a digit is expected), zero is written tovalueandfailbitis set. For signed integers, if extraction results in the value too large or too small to fit invalue,std::numeric_limits<T>::max()orstd::numeric_limits<T>::min()(respectively) is written andfailbitflag is set. For unsigned integers, if extraction results in the value too large or too small to fit invalue,std::numeric_limits<T>::max()is written andfailbitflag is set. π
π«Ά PARAMETERS π«Ά
valueβ reference to an integer or floating-point value to store the extracted value to.funcβ pointer to I/O manipulator function.sbβ pointer to the stream buffer to write all the data to.
π RETURN VALUE π
- Overloads (1)β(16), (18) β returns
*this. - Overload (17) β returns
func(*this).
π¦ 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. iykyk π§
π EXAMPLE π
The example says // Run this code, and honestly you should:
#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';
}
β OUTPUT β
n = 41
f = 3.14
b = false
hello world
π DEFECT REPORTS π
The following behavior-changing defect reports were applied retroactively to previously published C++ standards.
- LWG 64 β Applied to: C++98. Behavior as published: it was unclear whether overload (18) can only rethrow the
std::ios_base::failurethrown by callingsetstate(failbit). Correct behavior: all exceptions caught can be rethrown. - LWG 118 β Applied to: C++98. Behavior as published: overload (12,13) delegated the extraction to
num_get::get, but it does not have overloads forshortorint. Correct behavior: alongvalue is extracted instead ofshortorint. - LWG 413 β Applied to: C++98. Behavior as published: overload (18) only rethrew exceptions thrown while extracting characters from
sb, but characters are extracted from*this. Correct behavior: correctedsbto*this. - LWG 567 β Applied to: C++98. Behavior as published: overload (18) behaved as a
FormattedInputFunctionbecause of the resolution of LWG issue 60. Correct behavior: it behaves as anUnformattedInputFunction. - LWG 661 β Applied to: C++98. Behavior as published: overloads (12,13) did not store the extracted number to
valuedue to the resolution of LWG issue 118. Correct behavior: stores the number if no overflow occurs. - LWG 696 β Applied to: C++98. Behavior as published:
valuewas unchanged on extraction failure. Correct behavior: set to zero or minimum/maximum values.
π SEE ALSO π
okay bestie, here's the whole friend group β every one of these is worth a follow, fr fr:
operator>>(std::basic_istream)β the OOP version: extracts characters and character arrays β¨ (function template)operator<</operator>>β the duo that performs stream input and output on strings π (function template)operator<</operator>>β same energy but for bitsets β performs stream input and output of bitsets π (function template)operator<</operator>>β serializes and deserializes a complex number π¦ (function template)operator<</operator>>β performs stream input and output on a pseudo-random number engine πΈ (C++11) (function template)operator<</operator>>β performs stream input and output on a pseudo-random number distribution π (C++11) (function template)readβ extracts blocks of characters, the "gimme a whole chunk" icon π (public member function)readsomeβ extracts already available blocks of characters, the "gimme whatever's ready rn" vibe π (public member function)getβ extracts characters, one at a time, main character energy β (public member function)getlineβ extracts characters until the given character is found β literally the delulu line-reader we all know and love π (public member function)from_charsβ converts a character sequence to an integer or floating-point value, no stream drama, no cap π₯ (C++17) (function)
lowkey the whole point: if you're doing string stream stuff, these are your people. go say hi π«Ά
and that's a wrap on the whole page, bestie!! π this one came straight from http://cppreference.com, dated 2024.06.10 β so if the standard moves, blame the timestamps, not me π k bye, go write some operator>> overloads that actually compile πβ¨π
π¦ where this page comes from
- nixpkgs unstable: stdman 2024.07.05
- opensuse-leap 16.0: stdman 2024.07.05-bp160.1.9 Β· MIT
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms