The normal extraction of an integer value succeeds if the stream could read any integer value. That is, if there is at least one digit optionally followed by anything the read of an integer succeeds. The normal extraction operations don't try to read more, in particular they don't try to find the next whitespace.
From the sounds of it, you want to be sure that there is a whitespace following your number and fail if there is not. I can think of two different approaches to do this:
- Create a simple manipulator which checks for the stream being on a whitespace character. This, however, means that you would read your values using something like
in >> value >> is_space
.
- Create a custom
std::num_get<char>
facet, install it into a std::locale
, and imbue()
this std::locale
into your stream(s). It is a bit more involved but doesn't require any changes to the way integers are read.
Creating a manipulator like this is fairly trivial:
std::istream& is_space(std::istream& in)
{
if (!std::isspace(in.peek()))
{
in.setstate(std::ios_base::failbit);
}
return in;
}
Now, changing the way numbers are read is more interesting and I suspect I had just named a number of standard library classes most people are fairly unaware of. So, let's quickly type out an example for this as well. I will change the std::num_get<char>
facet only to deal with unsigned int
: to do it for other integral types it is necessary to override more functions. So, here is a replacement for the std::num_get<char>
facet:
class num_get:
public std::num_get<char>
{
iter_type do_get(iter_type it, iter_type end,
std::ios_base& ios, std::ios_base::iostate& err,
unsigned int& value) const
{
it = std::num_get<char>::do_get(it, end, ios, err, value);
if (it != end && !isspace(static_cast<unsigned char>(*it)))
{
err |= std::ios_base::failbit;
}
return it;
}
};
All this does is to derive a class from std::num_get<char>
and override one of its virtual functions. The implementation of this function is fairly straight forward: start with reading the value by delegating to the base class (I just realized that virtual functions indeed want to protected rather than private as I have though in the past but this is an entirely different discussion). Independent of whether this was successful (if it was not, it will have set up an error state in err) the override checks if there is another character available and, if so, checks if it is a space and if not sets a std::ios_base::failbit
in the error result err
.
What remains is to set up the stream to use this particular facet in a std::locale
and hook the new std::locale
into a stream:
std::locale loc(std::locale(), new num_get);
in.imbue(loc);
The std::locale
s and its facets are internally reference counted, i.e. you shouldn't keep track of the pointer to the facet and you don't need to keep the std::locale
around either. If it seems to be to cumbersome to imbue()
the created std::locale
or you want to use this modified logic everywhere, you can set the global std::locale
which is used to initialize any newly created stream to use the custom std::num_get<char>
facet.