I'll start with an example. There is such a mid() function for container classes, in particular for QVector . It allows you to get a cut from the middle of the container. In the description of the function is the following:
Returns a sub-vector of which contains elements from this vector, starting at position
pos. Iflengthis-1(the default), all elements afterposare included; if there are less thanlengthelements are included.
For the length, one special value -1 indicated, it is also said that if length greater than the real tail, then all remaining elements will be taken. What happens if the position is negative or greater than the actual size of the container, as well as with other negative lengths other than -1 is not clear. You have to go to the source and watch yourself:
QContainerImplHelper::CutResult QContainerImplHelper::mid(int originalLength, int *_position, int *_length) { int &position = *_position; int &length = *_length; if (position > originalLength) return Null; if (position < 0) { if (length < 0 || length + position >= originalLength) return Full; if (length + position <= 0) return Null; length += position; position = 0; } else if (uint(length) > uint(originalLength - position)) { length = originalLength - position; } if (position == 0 && length == originalLength) return Full; return length > 0 ? Subset : Empty; } All situations have already been processed here and, therefore, we really should not get a real way out of the array, and as a consequence of UB. But why is this not indicated in the “top” function description in the help center? It may be assumed that for some "specially optimized" cases these checks will not be? Or is Qt first of all the maximum fool protection?
So far, it turns out that if a person comes to the Qt world from pure C ++, he will hang up additional argument checks before the call, so as not to catch UB, although these checks are already inside the library. Thus, wasting time on unnecessary code. And the one who, on the contrary, started with Qt when switching to pure C ++, will receive the full program of all kinds of Access Violation in runtime.
In the end, I want to understand what all the same guide in the development of code, using the library Qt. Is it possible to rely on maximum security and parameter verification always and everywhere, or still, it is worthwhile to provide manual verification in some places, since Help does not contain sufficient information?