<p>I'm currently using the following code to right-trim all the <code>std::strings</code> in my programs:</p>

<pre><code>std::string s;
s.erase(s.find_last_not_of(" \n\r\t")+1);
</code></pre>

<p>It works fine, but I wonder if there are some end-cases where it might fail?</p>

<p>Of course, answers with elegant alternatives and also left-trim solution are welcome.</p>

In the case of an empty string, your code assumes that adding 1 to
```
string::npos
```
gives 0.
```
string::npos
```
is of type
```
string::size_type
```
, which is unsigned. Thus, you are relying on the overflow behaviour of addition.