[PATCH] Refactoring based on cppcheck suggestions
José Abílio Matos
jamatos at lyx.org
Mon Oct 5 14:27:39 UTC 2020
On Monday, October 5, 2020 1:52:19 PM WEST Jean-Marc Lasgouttes wrote:
> C++ stl uses unsigned for vector indices.
>
> JMarc
What the *man* has to say about that (pag. 123 from the article below):
" The most contentious part of getting span into C++20 turned out to be the
type of subscripts and sizes. The Core Guidelines span::size() was defined to
return a signed int as opposed to the unsigned used by the standard-library
containers. Similarly, subscripts were ints like for arrays, rather than
unsigned as they are for standard-library containers. This led to a revival of
an old and contentious issue:
• Some consider it obvious that subscripts, being non-negative, should be
represented as unsigned.
• Some consider consistency with standard-library containers more important
than any arguments about past mistakes related to unsigned.
• Some consider the use of unsigned to represent non-negative numbers
misguided (giving a false sense of safety) and a significant source of errors.
Over the strenuous objections of the original designers (including me) and
implementers of span, the second group won the votes, enthusiastically
supported by the first group, so std::span has unsigned sizes and subscripts.
I consider that a sad failure to take advantage of a rare opportunity to
remedy a nasty old mistake [Stroustrup 2018e]. Somewhat predictably and not
irrationally, the committee chose bug compatibility over the work of removing
a significant bug source.
But what could be wrong about representing subscripts as unsigned? This
appears to be a rather emotional topic. I received several hate mails about
this. There are two fundamental problems:
• unsigned does not model natural numbers: it has modular arithmetic and
subtraction. For example, if ch is an unsigned char, ch+100 will never
overflow.
• int and unsigned convert to each other at the slightest provocation, turning
negative values into huge signed values and vice versa. For example, -2<2u is
false; 2u is unsigned, so -2 is converted into a huge positive integer before
the comparison."
Thriving in a Crowded and Changing World: C++ 2006–2020
Bjarne Stroustrup
Proc. ACM Program. Lang., Vol. 4, No. HOPL, Article 70.
--
José Abílio
More information about the lyx-devel
mailing list