I have been wondering about the rationale behind the design of std::string
's substr(pos, len)
method for a while now. It still does not make sense to me, so I decided to ask the experts. The function throws a std::out_of_range
exception if the pos
argument exceeds the string length plus one. This can be inconvenient (even annoying) at times, but my real concern is consistency and the principle of least surprise. It turns out that the "end" position pos+len
of the substring is allowed to exceed the string length plus one. Disallowing this for the beginning but not for the end feels inconsistent to me. Allowing it for the end to me hints at the interpretation
return all characters at positions pos <= i < pos+len
however, then I would expect the function to return an empty string for values of pos
exceeding the string length, instead of throwing an exception. As a side note, with this interpretation it would even be sensible to allow for negative values of pos
(provided it had a signed type).
This leaves me with the following questions:
std::string
is not null-terminated and instead keeps track of the length of the string. If that's the true reason then personally I'd call that a very bad one.substr
in the future? I guess no, since silently breaking existing code is must worse than living with this twist...?This question really too opinion-based, but I will try to answer it point by point.
strncmp
-styled functions, but with such design you can just pass your buffer length for len
parameter and it will work fine. But, if you're trying to access substring that is located outside your string boundaries, then you probably missed some simple sanity checks. And internal implementation of std::string
doesn't matter.pos
exceeding size()
is defined in standard, so most likely no.My point is: this exception (though I prefer to never use those) allowes you to take notice of the code that missing some elementary sanity checks, like accessing the buffer outside it's boundaries. The same design is used in at()
-like functions and many other.