Comparing a string that might not exist?

This is a discussion on Comparing a string that might not exist? within the C++ Programming forums, part of the General Programming Boards category; I'm writing a parser that gets a line from a file and separates it (by spaces) into a string vector. ...

Comparing a string that might not exist?

I'm writing a parser that gets a line from a file and separates it (by spaces) into a string vector. Unfortunately there's no way to know how long the string will be. And to figure out what the program needs to do next, it has to compare the contents of the vector with literals.

at() is implemented in the standard library, which (hopefully) is more tested and bug free than your code. I would trust it over any other programmer's code any day.
So yes, it's good to have a second layer of defense, especially when performance is not of concern.

I didn't say a debug assertion isn't a second layer of defense, so long as it aborts in release builds too. I simply do not know of any implementations that does this expect for MSVC. That said, if they do, and if you can guarantee that your code will compiled on such a compiler, then it is an acceptable second layer of defense.
The at() method is guaranteed to work always, which is why I suggested it.

It isn't an acceptable response. It's a bug. But you wouldn't want your financial to be corrupted because of a "programming error."
Hence, the second layer defense is to stop such things from happening. But it doesn't relieve you of the responsibility to write correct and bug free code (it just means we are imperfect and introduce such bugs sometimes).

I simply do not know of any implementations that does this expect for MSVC. That said, if they do, and if you can guarantee that your code will compiled on such a compiler, then it is an acceptable second layer of defense.

Technically, you don't even need to be a compiler vendor to write the standard library. If you use STL-Port for example, you can get the same protection from any compiler that can compile it, see STLport: Debug Mode.

And if you are comfortable with a compiler vendor's implementation, googling "foo debug mode" can be enlightening. They probably call their implementation something different from the name of their compiler though.