fix many warnings, some cleanups

I have recently fixed hundreds of warnings that appear when building with non-standard warning settings.  I have retroactively split the changes into separate commits with meaningful commit messages, in order to facilitate review (and possibly cherry-picking).

Of course, this still does not fix all warnings, but let's discuss this first, before spending too much time on doing this in even larger parts of the library.

(Yes, some of these non-standard warnings are annoying, but fixing them *does* improve overall code quality and from time to time uncovers bugs.  Also, signedness in C++ is just broken, so we obviously have to work around that.)