On Feb 17, 6:18*pm, James Kanze <james.ka...@gmail.com> wrote:
> On Feb 16, 7:41 pm, Ángel José Riesgo <ajrie...@gmail.com> wrote:
>
>
>
> > I'm writing some code that parses a string and tries to find some
> > tokens and extract some data from the string. The problem is simple
> > and the code I've just written works fine. However, I need some ugly
> > casts to get rid of a signed/unsigned mismatch warning, and I was
> > wondering if there may be a more elegant way of doing this. A dumbed
> > down version of my code follows:
> > #include <string>
> > const std::string kToken = "TOKEN";
> > void FindToken(std::string::const_iterator& position,
> > std::string::const_iterator end)
> > {
> > * * * * std::string::size_type tokenLength = kToken.length();
> > * * * * if(tokenLength <= (end - position)) // <- Signed - unsigned
> > conversion warning here.
> > * * * * {
> > * * * * * * * * std::string expectedToken(position, position + tokenLength);
> > * * * * * * * * if(expectedToken == kToken)
> > * * * * * * * * * * * * position += tokenLength; // The token has been found and the
> > iterator is advanced before returning.
> > * * * * }
> > }
> > Basically, I've moved the bit of code I'm interested in to the above
> > function FindToken, which tries to find a certain token ("TOKEN"), and
> > advances the "position" iterator by the token's length if it is found.
> > Otherwise, the function returns quietly leaving the iterator
> > unchanged. In the actual code, I can assume that the two iterators
> > come from the same string object and that position <= end.
> > Now the problem with the above code (building it with MSVC 10) is that
> > I get a warning because of the conversion between the signed type
> > returned by the (end - position) iterator subtraction and the
> > std::string::size_type unsigned integer type. I can
> > static_cast<std::string::size_type> the warning away, of course, but
> > it's a bit ugly, so I was wondering if anyone knows of a way of doing
> > this sort of thing without either warnings or casts.
>
> Using int instead of std::string::size_type should get rid of
> the error. *But you're doing a lot of extra work; my version
> would be just:
>
> * * if ( static_cast<size_t>(end - position) >= kToken.size()
> * * * * * * && std::equal(kToken.begin(), kToken.end(), position) )
> * * * * position += kToken.size();
>
> (Here, you need the static_cast, because of a design flaw in the
> standard library; kToken.size() should return int.)
>
> --
> James Kanze
On Feb 17, 6:18 pm, James Kanze <james.ka...@gmail.com> wrote:
> On Feb 16, 7:41 pm, Ángel José Riesgo <ajrie...@gmail.com> wrote:
>
>
> (Here, you need the static_cast, because of a design flaw in the
> standard library; kToken.size() should return int.)
>
kToken.size() should return int specifically?
What if int is smaller than std::string::difference_type?
Does in mean there shouldn't be a container::size_type, and it should
all be container::difference_type?
Is there any good reason why there is such a distinction in the
standard containers?
itaj
|