Path: csiph.com!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail From: Bo Persson Newsgroups: comp.lang.c++ Subject: Re: overuse of auto is dangerous Date: Fri, 13 Mar 2020 03:17:54 +0100 Lines: 39 Message-ID: References: Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit X-Trace: individual.net njdW9EWFXENfLTt2dR1rMAgspcLdUHr5Fg1Q9WSEkJC9p1mPrY Cancel-Lock: sha1:NkhQ4BbWFQwP1PDbsTeboy4ss+k= User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; rv:68.0) Gecko/20100101 Thunderbird/68.5.0 In-Reply-To: Content-Language: sv Xref: csiph.com comp.lang.c++:72250 On 2020-03-13 at 00:27, jacobnavia wrote: > Consider: > > auto f = foo(); > > What is "f"? Easy, "f" is the return value of foo(). Probably some kind of iterator that you are just going to pass to one or more std::algorithm, so you don't really care to spell out its name. > > Ahhh you have to figure out the return type of foo() of course. Great, > there you go to your favorite IDE and try to jump to the correct > definition of the foo() method in the correct class with the correct > overload resolution... > > Even worst... when you change the return type of foo() from type A to > type Z, all the "auto" constructs will continue to compile AUTOmatically > of course. Problem is, the type of "f" will have changed and that can > provoke incomprehensible compile time errors further down or even run > time errors in the form of access violation or whatever! > > Wouldn't it be better to write: > > TYPE_A F = foo(); You mean like typename std::unordered_map, std::equal_to, special_allocator>>::const_iterator F = foo(); Bo Persson