On Fri, 7 Aug 2020 at 15:08, Luis Machado via Gcc <gcc@gcc.gnu.org> wrote: > > Hi, > > cc-ing the GCC mailing list, as we may want to use the same coding style > for GDB and GCC. > > Yesterday I brought this topic up on IRC. I notice we started using more > and more the "auto" keyword. In some cases, this is actually useful and > makes the code a bit more compact. GDB has been using those more often, > whereas GCC, for example, isn't using those too much. > > Looking at the coding standards for GCC > (https://gcc.gnu.org/codingconventions.html), I don't see anything > dictating best practices for "auto" use. > > I guess it is a consensus that "auto" is a good fit when dealing with > iterators, lambda's and gnarly templates (but only when the type is > already obvious from its use).
GCC only moved to C++11 very recently, so it's unsurprising. > There are other situations where "auto" may make things a little more > cryptic when one wants to figure out the types of the variables. One > example of this is when you have a longer function, and you use "auto" > in a variable that lives throughout the scope of the function. This > means you'll need to go back to its declaration and try to figure out > what type this particular variable has. > > Pedro has pointed out LLVM's coding standards for "auto", which we may > or may not want to follow/adopt: > https://llvm.org/docs/CodingStandards.html#use-auto-type-deduction-to-make-code-more-readable > > It sounds like a reasonable idea to me. Thoughts? It seems like common sense to me. "Almost always use auto" is a silly guideline. I can't stand seeing nonsense like: auto main() -> int { ... } > Are there other C++ constructs people think would benefit from a more > formal style guideline? As we move to newer C++ standards over time, it > is more likely we will start using newer constructs, and some of those > may make the code potentially less readable. I'm also not a fan of "always use {} for init" rules. There are times when using {} for initialization is necessary, or more convenient (e.g. it avoids ambiguities in the grammar that would need clunky workarounds to avoid otherwise) but that doesn't make it always better. On the other hand, I do like: - nullptr instead of NULL - constructors and assignment ops defined with = default - default member initializers i.e. struct X { void* p = nullptr; size_t num = 0; }; (especially useful in GCC code where leaving members uninitialized seems to be the norm)