Question

How come istringstream can't seem to fully read numeric literals with suffixes?

#include <iostream>
#include <sstream>
using namespace std;

int main() {
    long long x = 123ULL; // shows 123ULL is a valid long long literal

    istringstream iss("123ULL");

    iss >> x;
    cout << "x is " << x << endl;

    char extra;
    iss >> extra;
    cout << "remaining characters: ";
    while(!iss.eof())
    {
        cout << extra;
        iss >> extra;
    }
    cout << endl;

    return 0;
}

The output of this code is

x is 123
remaining characters: ULL

Is this behavior controlled by the locale? Could anyone point me to clear documentation on what strings are accepted by istringstream::operator>>(long long)?

Était-ce utile?

La solution

Yes, it's controlled by the locale (via the num_get facet), but no locale I ever heard of supports C++ language literals, and it would be the wrong place to customize this.

Streams are for general-purpose I/O, and C++ integer literal suffixes are very specialized.

The exact behavior of the default num_get facet is described in the C++11 standard in section 22.4.2.1. The description partially references the strto* family of functions from the C standard library. You can find a somewhat condensed version here:

http://en.cppreference.com/w/cpp/locale/num_get/get

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top