kicad-developers team mailing list archive
-
kicad-developers team
-
Mailing list archive
-
Message #40039
Re: "auto" policy
Hi,
I think auto is useful when it avoids repetition of useless things,
and doubly so for things where the type is unwieldy, like iterators,
and, particularly, lambdas. For example:
auto up = std::make_unique<TYPE>( ... ); // or make_shared
auto it = aVec.begin(); // or cbegin or returns from std::find, etc
auto lambda = []() { return 42; }; // only Cthulu knows how to type this
for( const auto& x : container ); // where x is not a "usual" type,
and not immediately clear
auto& item = static_cast<ITEM&>( other ); // ITEM& is clearly the type here
For places were the type is *not* obvious from the immediate context
(probably the same line), I do not suggest auto. Basically any where
you'd need to check the function signature. (I am probably guilty a
few of these):
auto x = item.GetX(); // what is X? int? float? double? type-safe
dimension? a class?
Also I think that if we are putting a pointer in an auto, we should
use the "star" to make this clear, as KiCad (IMO rightfully) does not
make pointer-ness clear with something like Hungarian notation. I.e.
"plain auto" means "a thing", for example, int, a class, or
std::unique_ptr. "auto*" means a pointer and "auto&" is a reference to
a thing.
auto* newed = new TYPE(); // but prefer unique_ptr where sensible
auto* item = thing.GetItem<ITEM>(); // this could be null
auto& thing = thing.GetThing(); // this definitely exists
I think there is a (weak) case to be made for the following
construction, which disallows uninitialised variables, but "looks
funny":
auto x = size_t{ 42 };
size_t x = 42; // can lose "= 42" and still compile.
But compilers and static analysers are usually fairly "on it" with
uninitialised variables these days, so it's not that exciting, and
it's certainly not a common idiom.
TL;DR, IMO, use auto only when it is clearer than writing the type out.
Cheers,
John
On Fri, Apr 5, 2019 at 12:00 PM jp charras <jp.charras@xxxxxxxxxx> wrote:
>
> Le 05/04/2019 à 12:39, Jeff Young a écrit :
> > Do we have a policy on when to use auto?
> >
> > Personally, I like it when it removes repetition. For instance:
> >
> > auto pin = new SCH_PIN();
> >
> >
> > And I like it when it removes stupid template things like std::pair<>:
> >
> > for( auto it : my_unordered_map )
> >
> >
> > But I don’t like it in other cases. This reduces readability for me:
> >
> > auto pos = pin->GetPosition();
> >
> >
> > However, I’m just one voice, and the idea of when something is a “stupid
> > template thing” and when it isn’t is admittedly pretty ill-defined.
> >
> > How do others feel?
> >
> > Cheers,
> > Jeff.
>
> I am thinking auto should be not used for basic types (bool, int ...)
> and very usual types (wxPoint, wxString, std::string) because it
> obfuscate these types without benefit.
> Of course, obfuscation depends of the context.
>
> But I agree it is really very useful for iterators.
>
> --
> Jean-Pierre CHARRAS
>
> _______________________________________________
> Mailing list: https://launchpad.net/~kicad-developers
> Post to : kicad-developers@xxxxxxxxxxxxxxxxxxx
> Unsubscribe : https://launchpad.net/~kicad-developers
> More help : https://help.launchpad.net/ListHelp
References