CppCon 2018: Staffan Tjernström “Almost Always Avoiding auto”

preview_player
Показать описание


Lightning Talk


*-----*
*-----*
Рекомендации по теме
Комментарии
Автор

There is a proposal to add concept-constrained-auto to the language, so you can write things like *auto<Iterator> i = c.begin()* so you still get a deduced type, but you are spelling out the salient properties of the type that you are interested in to the reader. (Obviously, Iterator is a concept.)

oktal
Автор

Guy: Never use auto! * proceeds with examples that no sane developer would actually use in their code while leaving out many other very legit usecases *

zzptichka
Автор

I just encountered `auto` instead of `bool` in the code. This is infuriating. Stop turning C++ into JavaScript!

PaladinPL
Автор

This entire talk is using slight of hand essentially, since what he is actually arguing against is using deduced return types everywhere, which isn't very controversial. It has very little to do with using auto for declaring variables, since he doesn't present any points against it.

He then proceeds to argue against using pointer qualified auto, like auto*, which I also agree with. There's a very simple workaround though, and that is not to use that.

His final point is that auto is confusing because it discards cv and ref qualifiers, which is a deliberate design decision. His highly specific example where it causes a bug may appear to be convincing, except that this decision was made to prevent a whole other set of much more likely bugs involving dangling references.

themeeman
Автор

if your functions are defined with auto as return, use trailing return type. visual studio code with the c++ extension has no issue resolving types. all you have to do is hover

minciNashu
Автор

The cynical view that "auto exists to enable slideware" can hardly be torn down by additional slideware. Surely it's more productive to present real and common problems with the AAA style than a pathological and hardly real case of a chain of deduced-type template functions.

JosV
Автор

Double-edged sword. Auto can help (e.g. with iterators and template code) but it can also obscure code.

gast
Автор

I'm an ANA guy. Almost Never Auto.

zhulikkulik
Автор

or you can just use an ide and it'll tell you the type

qsvui
Автор

I don't get this. In the example where a long dependency of functions are deduced via auto return type, what happens when you have to go back and change the return type from uint64_t to... idk, int? Now everything down the chain has to be refactored, leading to a messy diff, and potentially unwanted casts. Additionally, you're now getting warnings for comparing signed and unsigned integers, implicit conversion of an unsigned int into to a signed int, truncation, etc. And if you ignore this type of thing, you could run into problems down the road where perhaps a -1 becomes 2^32-1 because you changed a type in one spot and missed something downstream.

My counter argument for this is that it is good practice to not use code duplication, so there should only be one place where the type is defined. If you don't want to dig through a bunch of function calls to find a type, stop chaining function calls so deep, or start using an IDE.

keris
Автор

It doesn't happen often that I don't agree with anything said on a cppcon video. A presenter talking about avoiding auto on a modern c++ conference is odd to me. Very odd. As Bjarne put it more clearly: programming C++ is about the algorithms, not the types.

ruadeil_zabelin
Автор

Nice counterpoints for AAA in practical modern C++ coding.

steveragnar
Автор

problem about auto are the functions. that have deduced return type without any enforcement of concept or anything. if functions themselves could stand on their own at all times and represent exactly to the user what they are returning it would be much better. but yea, in my opinion deduced functions should be a rarity. and problem is solved.

TaiDesHen
Автор

So Herb Sutter says, ALWAYS use auto whenever possible, and this guy says never use it... Guess who I believe. I personally do not use auto but rarely but that does not mean I will not follow this guys advice.

seditt
Автор

That's silly. Presenter doesn't know what he's talking about.

MaceUA
Автор

funny how this "expert" managed to make a noob mistake and write auto i: instead of auto& i: and does know how to use IDEs that tell you what auto type is.

Voy