[Development] RFC: more liberal 'auto' rules?

André Pönitz apoenitz at t-online.de
Fri Dec 4 23:43:03 CET 2015


On Fri, Dec 04, 2015 at 04:33:26PM -0500, Matthew Woehlke wrote:
> On 2015-12-04 15:35, André Pönitz wrote:
> > On Fri, Dec 04, 2015 at 08:01:59PM +0100, Olivier Goffart wrote:
> >> and it's one reason less to make errors:
> >> Using 'int' instead of 'quint64' or 'size_t', or QString instead of QByteArray 
> >> is way to frequent. (and the compiler won't complain)
> > 
> > ["QString instead of QByteArray" does not happen with proper
> > QT_NO__CAST_*_ASCII settings.  Not to mention the cases where mindless
> > replacement of explicit types by 'auto' doesn't produce identical results.]
> 
> But wrongly mixing integer types *does* happen. I speak from personal
> experience based on a code base that has a relatively large mess of
> *exactly* such errors.
> 
> > Code is typically read more often than changed. Targeting at making
> > specific refactorings easier is optimizing the wrong utility function.
> 
> Which of these is easier to read? (More importantly, which is easier to
> *verify* that it is correct?)
> 
>   for (int i = 0; i < list.size(); ++i)
>     foo(list[i]);

Whether the access is correct depends on the type of list, which you don't
disclose.

In any case, this loop follows a well-known pattern.

>   for (auto i : qtIndexRange(list.size())
>     foo(list[i]);


Whether the access is correct depends on the type of list, which you don't
disclose.

In any case, this is an uncommon pattern, using some unknown qtIndexRange()
function. Moreover it is more to type. The extra level of parantheses makes
it harder to parse for humans, introducing an aditional source of errors,
which you nicely demonstrated by making the example non-compilable.

> Which is *really* more meaningful?

The first one.

> "The type of 'i' is 'int', and I
> really, really hope that 'list' is actually indexed by 'int'", or "the
> type of 'i' is the index type of 'list'¹"?
> 
> Do you really *care* what is the type of 'i'?

Yes, I do care about types, almost always.

> Or do you care that it is
> the *correct* type. I (and others) submit that the latter is more valuable.

Your message has been heard. I do not agree with you.

I feel more comfortable arguing about whether a type is the right one
if the type is known than when it is unknown.

There *are* some sensible uses of 'auto', those are pretty much the ones
listed in Creator's coding-style.qdoc. Overuse of 'auto' reduces
code readability and maintainability, I would not like to see the Qt
code base move there.

Andre'



More information about the Development mailing list