Path: csiph.com!eternal-september.org!feeder.eternal-september.org!reader01.eternal-september.org!.POSTED!not-for-mail From: Tim Rentsch Newsgroups: comp.lang.c++ Subject: Re: Almost Always Auto? (was Re: Substitution failure can be an error) Date: Thu, 28 Nov 2019 12:46:49 -0800 Organization: A noiseless patient Spider Lines: 27 Message-ID: <86tv6nafuu.fsf@linuxsc.com> References: Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Injection-Info: reader02.eternal-september.org; posting-host="dce185ee50ea005ee4a34884a21fb89a"; logging-data="26629"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/5w1gHMr2cBBwzxBNwOqASAT7Ar054tK8=" User-Agent: Gnus/5.11 (Gnus v5.11) Emacs/22.4 (gnu/linux) Cancel-Lock: sha1:5Yc+2226Ar3Y/sLyhs6UumJr0XM= sha1:GSBCYIB+5T7VC20YVqei8xX6hYY= Xref: csiph.com comp.lang.c++:70091 Jorgen Grahn writes: > On Fri, 2019-11-22, Vir Campestris wrote: > >> When I read "auto x = somefunction()" I don't know what x is. Unless I >> look at the declaration. Which if I'm unlucky is one of several >> overloads with template type deduction from the parameters. >> >> But IMHO reading matters more. You'll only write it once. > > I tend to agree. I use auto, but try to be careful so it won't be a > problem when I revisit the code, like my duck typing exercises are in > Python. I don't have a lot of experience with "auto" in C++, but a fair amount of experience with type inference (and not needing type annotations in many cases) in other programming languages. I don't have any hard and fast rule, but generally I find it works well to give an explicit type annotation only in those cases where it's important to know exactly what the type is. Probably this rule carries over into C++ reasonably well, although the two situations are not completely parallel. In particular, C++ doesn't have partial types, except maybe for arrays -- it's pretty much all or nothing, either a completely specified type or none at all; there is no way in C++ to say a type is "a list of something, but I won't say what the something is". But I think the general rule is still good in the C++ domain.