Why the istringstream eof flag does not become true when the boolean string value is successfully converted to bool?

I am learning how to convert values ​​stored as strings to native types using istringstream. When a number stored as a string is successfully converted to intor double, the istringstream eof () function returns true. When a boolean stored as a string is successfully converted to a value bool, eof () returns false.

What makes the difference and why does eof () not return true if there seem to be no other remaining characters to process?

Code to convert to bool:

string value = "true";
istringstream converter(value);
bool convertedValue;

if (!(converter >> std::boolalpha >> convertedValue)){
    cout << "Conversion error." << endl;
} else {
    cout << "Conversion success." << endl;
}

cout << "convertedValue=" << convertedValue << "  value.length()=" << value.length() << "  converter.tellg()=" << converter.tellg() << "  converter.eof()=" << converter.eof() << endl;

The result shows that the eof flag is false:

Conversion success.
convertedValue=1  value.length()=4  converter.tellg()=4  converter.eof()=0

Code to convert to double:

string value = "1234.56";
istringstream converter(value);
double convertedValue;

if (!(converter >> std::boolalpha >> convertedValue)){
    cout << "Conversion error." << endl;
} else {
    cout << "Conversion success." << endl;
}

cout << "convertedValue=" << convertedValue << "  value.length()=" << value.length() << "  converter.tellg()=" << converter.tellg() << "  converter.eof()=" << converter.eof() << endl;

The output shows that the eof flag is true:

Conversion success.
convertedValue=1234.56  value.length()=7  converter.tellg()=-1  converter.eof()=1

Code to convert to int:

string value = "1234";
istringstream converter(value);
int convertedValue;

if (!(converter >> std::boolalpha >> convertedValue)){
    cout << "Conversion error." << endl;
} else {
    cout << "Conversion success." << endl;
}

cout << "convertedValue=" << convertedValue << "  value.length()=" << value.length() << "  converter.tellg()=" << converter.tellg() << "  converter.eof()=" << converter.eof() << endl;

The output shows that the eof flag is true:

Conversion success.
convertedValue=1234  value.length()=4  converter.tellg()=-1  converter.eof()=1

g++ (Debian 4.8.3-3) 4.8.3.

+4
2

" " , .

.

( "" ) "e". , .

, : , tellg -1 .

+3

++

[in, end) (. 23.2.3) .

, ,

#include <iostream>
#include <iomanip>
#include <sstream>
#include <string>

int main() 
{
    std::istringstream is( "trueabc" );
    bool b = false;

    is >> std::boolalpha >> b;

    std::cout << std::boolalpha << b << std::endl;

    std::cout << is.eof() << std::endl;

    std::string s;

    is >> s;

    std::cout << s << std::endl;

    return 0;
}

GCC ( www.ideone.com) MS V++ 2010,

true
false
abc

, "true" , " ".

, , , MS V++ 2010 .

#include <iostream>
#include <iomanip>
#include <sstream>

int main() 
{
    std::istringstream is( "true" );
    bool b = false;

    is >> std::boolalpha >> b;

    std::cout << std::boolalpha << b << std::endl;

    std::cout << is.eof() << std::endl;


    return 0;
}

MS V++ 2010:

true
true

GCC:

true
false
+1

Source: https://habr.com/ru/post/1544575/


All Articles