operator>>(std::basic_istream)

Header: <istream>

1,2) Performs character input operations.

# Declarations

template< class CharT, class Traits >
basic_istream<CharT, Traits>&
operator>>( basic_istream<CharT, Traits>& st, CharT& ch );
template< class Traits >
basic_istream<char, Traits>&
operator>>( basic_istream<char, Traits>& st, signed char& ch );
template< class Traits >
basic_istream<char, Traits>&
operator>>( basic_istream<char, Traits>& st, unsigned char& ch );
template< class CharT, class Traits>
basic_istream<CharT, Traits>&
operator>>( basic_istream<CharT, Traits>& st, CharT* s );
template< class Traits >
basic_istream<char, Traits>&
operator>>( basic_istream<char, Traits>& st, signed char* s );
template< class Traits >
basic_istream<char, Traits>&
operator>>( basic_istream<char, Traits>& st, unsigned char* s );

(until C++20)

template< class CharT, class Traits, std::size_t N >
basic_istream<CharT, Traits>&
operator>>( basic_istream<CharT, Traits>& st, CharT (&s)[N] );
template< class Traits, std::size_t N >
basic_istream<char, Traits>&
operator>>( basic_istream<char, Traits>& st, signed char (&s)[N] );
template< class Traits, std::size_t N >
basic_istream<char, Traits>&
operator>>( basic_istream<char, Traits>& st, unsigned char (&s)[N] );

(since C++20)

template< class Istream, class T >
Istream&&
operator>>( Istream&& st, T&& value );

(since C++11)

# Parameters

# Notes

Extracting a single character that is the last character of the stream does not set eofbit: this is different from other formatted input functions, such as extracting the last integer with operator», but this behavior matches the behavior of std::scanf with “%c” format specifier.

# Example

#include <iomanip>
#include <iostream>
#include <sstream>
 
int main()
{
    std::string input = "n greetings";
    std::istringstream stream(input);
 
    char c;
    const int MAX = 6;
    char cstr[MAX];
 
    stream >> c >> std::setw(MAX) >> cstr;
    std::cout << "c = " << c << '\n'
              << "cstr = " << cstr << '\n';
 
    double f;
    std::istringstream("1.23") >> f; // rvalue stream extraction
    std::cout << "f = " << f << '\n';
}

# Defect reports

DRApplied toBehavior as publishedCorrect behavior
LWG 13C++98the definition of n mentioned a non-existing name eosreplaced with CharT()
LWG 68C++98no null characters were stored at the end of the output for overload (2)stores a null character
LWG 1203C++98overload for rvalue stream returned lvalue reference to the base classreturns rvalue referenceto the derived class
LWG 2328C++98overload for rvalue stream required another argument to be lvaluemade to accept rvalue
LWG 2534C++98overload for rvalue stream was not constrainedconstrained

# See also