Namespaces
Variants
Views
Actions

std::basic_istream::sentry

From cppreference.com
< cpp‎ | io‎ | basic istream
Revision as of 21:11, 2 November 2012 by P12bot (Talk | contribs)

Template:cpp/io/basic istream/sentry/navbar Template:ddcl list begin <tr class="t-dsc-header">

<td>
Defined in header <istream>
</td>

<td></td> <td></td> <tr class="t-dcl-sep"><td></td><td></td><td></td></tr>

<tr class="t-dcl ">

<td class="t-dcl-nopad">
template< class CharT, class Traits = std::char_traits<CharT>>
class std::basic_istream<CharT, Traits>::sentry;
</td>

<td class="t-dcl-nopad"> </td> <td class="t-dcl-nopad"> </td> </tr> Template:ddcl list end

An object of class basic_istream::sentry is constructed in local scope at the beginning of each member function of std::basic_istream that performs input (both formatted and unformatted). Its constructor prepares the input stream: checks if the stream is already in a failed state, flushes the tie()'d output streams, skips leading whitespace if skipws flag is set, and performs other implementation-defined tasks if necessary. All cleanup, if necessary, is performed in the destructor, so that it is guaranteed to happen if exceptions are thrown during input.

Contents

Member types

traits_type Traits

Member functions

Template:cpp/io/basic istream/sentry/dcl list constructorTemplate:cpp/io/basic istream/sentry/dcl list operator bool
finalizes the stream object after formatted input or after exception, if necessary
(public member function)
operator=
[deleted]
not copy assignable
(public member function)

Example

#include <iostream>
#include <sstream>
 
struct Foo {
   char n[5];
};
std::istream& operator>>(std::istream& is, Foo& f)
{
    std::istream::sentry s(is);
    if(s)
        is.read(f.n, 5);
    return is;
}
 
int main()
{
    std::string input = "   abcde";
    std::istringstream stream(input);
    Foo f;
    stream >> f;
    std::cout.write(f.n, 5);
    std::cout << '\n';
}

Output:

abcde


See also

Template:cpp/io/basic istream/dcl list operator gtgtTemplate:cpp/io/basic istream/dcl list operator gtgt2