Andrzej Krzemienski wrote:
5. Compiling with -Wall gives me a warning in GCC when sorting an empty hub: a zero-sized heap array is allocated: https://godbolt.org/z/dWvxv9n8s This is technically fine to allocate such arrays, but maybe it is worth to make -Wall clean.
The warning isn't caused by the zero sized allocation (which is legal.)
It's because the compiler can see that the array has zero elements, but can't see that the loop inside this call to visit
https://github.com/joaquintides/hub/blob/063cf1d3589d4893b53964084ee 56f4476eed471/include/boost/container/hub.hpp#L1628
is never executed. So it says "if this loop is executed, it would be UB because there aren't any elements there" but this is a false positive because the loop isn't executed.
(The subsequent loops suffer from the same issue and also cause warnings for the same reason.)
A common problem with GCC, and unfortunately nothing can be done about it short of disabling the warning.
On second thought, given that the warning only fires on empty containers, it might be possible to avoid if `sort` checks for `empty()` (or `size() <= 1`) and returns. Adding a branch to avoid a warning is a bit dubious in general, but in this case it avoids a completely unnecessary allocation as well, so it's sound even in principle. :-)