logoalt Hacker News

How well do you know C++ auto type deduction?

81 pointsby volatileintlast Tuesday at 10:44 PM105 commentsview on HN

Comments

am17antoday at 3:13 PM

Usually codebases disallow auto because without an IDE it's difficult to see the type. I think this reduces the cognitive load of C++ a bit. The only time it is allowed is getting iterators types from STL containers.

I remember fretting about these rules when reading Scott Meyer's Effective C++11, and then later to realize it's better not to use auto at all. Explicit types are good types

show 5 replies
dataflowtoday at 6:47 AM

This makes things seem more complicated than they already are, I feel?

There's nothing special about auto here. It deduces the same type as a template parameter, with the same collapsing rules.

decltype(auto) is a different beast and it's much more confusing. It means, more or less, "preserve the type of the expression, unless given a simple identifier, in which case use the type of the identifier."

Suractoday at 7:09 AM

I always try to avoid auto in c++ or var in C#. On the paper it is a nice was to save you from typing out the type but this is only true if you have tool support and can use your mouse to obtain the type. In printouts or even in a text snippet I’m am lost. I think auto inC++ was the try to shorten some overburden type constructs and make it a little more friendly to use heavy templating. Please forgive my bad English. I’m no native speaker

show 7 replies
jandrewrogerstoday at 6:56 AM

I knew the answer to most of these intuitively but the story isn’t great. Regardless of the programming language, I’ve always been an “auto” minimalist. There are relatively few contexts where relying on inference is justified by the expedience. Ignoring the issues raises by the article, explicitness simplifies things and reduces bugs.

That said, there are some contexts in which “auto” definitely improves the situation.

show 1 reply
tigranbstoday at 9:48 AM

The last time I worked meaningfully with C++ was back in 2013. Now that I write mostly Rust and TypeScript, I'm amazed by how C++ has changed over the years!

Regarding the "auto" in C++, and technically in any language, it seems conceptually wrong. The ONLY use-case I can imagine is when the type name is long, and you don't want to type it manually, or the abstractions went beyond your control, which again I don't think is a scalable approach.

show 1 reply
physicsguytoday at 8:42 AM

Auto is fine where the context is obvious, I think. For e.g.:

    void func(std::vector<double> vec) {
        for (auto &v : vec) {
            // do stuff
        }
    }

Here it's obvious that v is of type double.
show 1 reply
flakestoday at 6:16 AM

Auto has really made c++ unapproachable to me. It's hard enough to reason about anything templated, and now I frequently see code where every method returns auto. How is any one supposed to do a code review without loading the patch into their IDE?

show 4 replies
gpderettatoday at 9:48 AM

One of the craziest bugs I have had in C++ was due to auto, and it would only trigger when Trump would announce tariffs. It would have been completely preventable if I had paid full attention to my IDE feedback or the correct compiler flags were set.

show 1 reply
andrepdtoday at 10:16 AM

Things like this are why I don't get when anyone calls Rust "more difficult than C++". I don't think I've ever encountered a more complex and cumbersome language than C++.

show 1 reply
antonvstoday at 7:08 AM

“How well do you know Latin grammar rules?”

show 1 reply
CalChristoday at 5:24 AM

Apparently the C++ standard calls this type deduction. But I've always called it type inference.

show 4 replies
112233today at 6:10 AM

Saving this to use as an argument when C++ comes up in a discussion. This toxic swamp cannot be fixed and anyone chosing to jump into it needs a warning.

show 1 reply