Hacker News new | past | comments | ask | show | jobs | submit login

In the interests of perpetuating this endless flamewar, here's Herb Sutter saying C++ programmers should use auto 'by default'.

(I see my snarky comment there got no reply.)

https://softwareengineering.stackexchange.com/a/180616/




Herb Sutter is biased. He works on C++ language lawyering, he works on new features, he works in the STL, etc.

People like him tend to be biased about using auto because they write mostly libraries and generic ones at that (data structures, for instance).

In most code out there you actively avoid templates if possible, so that code is concrete, compiles faster and is easier to debug.


Well, there's two ways to write an application, analogous to two styles in mathematics as described by A. Grothendieck.

http://www.landsburg.com/grothendieck/mclarty1.pdf


Bingo! Writing a template library is totally different to writing an application.


Another reason for C/C++ is that they are very permissive with implicit lossy conversions. If you specified explicit type, chances are the compiler helpfully made a lossy conversion for you.


Sutter made that point under Maintainability and robustness.

Like Sutter's answer, this point doesn't answer the complaint. People on the anti-auto side say it seriously harms readability, as locals' types are no longer clear at a glance. They aren't asking for a list of reasons why some people favour auto, they're asking for an answer to their readability problem.

Perhaps IDEs could infer types and display them as a superscript. That would keep just about everyone happy. (Perhaps not Vim users.)


Herb didn't say it well enough. It's a tradeoff between correctness and readability. If it wasn't for C++'s weak type system you wouldn't be required to make this tradeoff and could improve readability while still staying correct.


In IDE-friendly languages like Kotlin, you can enable showing of inferred types, e.g. https://i.stack.imgur.com/tiqjc.png


Perfect, just what I was thinking. Does something like that exist for C++? Visual Studio 2019 doesn't seem to have it, but it's able to show a local's type when I hover over the local's identifier.


If you use only explicit constructors and ban cast operators except for the most basic of types, then you have a sane language and explicitness in your code.


Sounds sensible. Google seems to agree with you. https://google.github.io/styleguide/cppguide.html#Implicit_C...

LLVM's coding standard though doesn't seem to have anything to say about implicit conversions: https://llvm.org/docs/CodingStandards.html


It's sane on C++ scale, but on absolute scale that sanity is still wanting, Sutter mentions this.


In the context of C++, the use of auto allows to avoid unintended type conversions during initialization.




Consider applying for YC's Summer 2025 batch! Applications are open till May 13

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: