Subject: Re: [boost] usage of auto in tutorials
From: Gavin Lambert (boost_at_[hidden])
Date: 2019-03-19 23:19:33
On 20/03/2019 10:08, Andrzej Krzemienski wrote:
>> I subscribe to rule "almost never auto"; but by this I mean "do not use
>> type inference when you can spell out your type explicitly".
> I never suspected that the feature known as "trailing return type", like
> auto Class::fun() -> value_type;
> would be questioned as unfit for tutorials. I am quite surprised that in
> Stephan's environment "auto" is banned regardless if it is used for type
> inference or for trailing return types.
Interestingly, I have the exact opposite rule.
I never ever use trailing return types (unless absolutely required by
arcane template rules), as I don't believe they provide any clarity (and
in fact provide more obscurity for those less familiar with the syntax).
Meanwhile in general I consider using auto for type inference a very
good thing, especially in the context of "don't particularly care"
nested types such as iterators. (There are a few exceptions, where it
is useful to specify the type explicitly so that the compiler will warn
you if someone later changes the type. And I never use it with
primitive value types (int/float), as that's the most dangerous source
of type surprises.)
I'm also fond of using auto to DRY when calling factory methods such as
make_unique and make_shared.
For the purposes of writing a tutorial, however, I would encourage
minimising use of auto (if not avoiding it altogether), both for
explicitness of the example (which aids readability when you don't have
an IDE doing auto-complete for you) and because if it becomes too
cumbersome to spell out the types in your own examples, it might be a
sign that your type names are wrong.