public inbox for gdb@sourceware.org
 help / color / mirror / Atom feed
From: Jonathan Wakely <jwakely.gcc@gmail.com>
To: Luis Machado <luis.machado@linaro.org>
Cc: "gdb@sourceware.org" <gdb@sourceware.org>,
	gcc Mailing List <gcc@gcc.gnu.org>,
	Pedro Alves <palves@redhat.com>,  Simon Marchi <simark@simark.ca>
Subject: Re: Coding style for C++ constructs going forward
Date: Fri, 7 Aug 2020 19:21:49 +0100	[thread overview]
Message-ID: <CAH6eHdSrmOh47Qf6XxpRmup-AM_Dn_CGo44t-JRXg5z2JF_-1g@mail.gmail.com> (raw)
In-Reply-To: <33412819-8a5e-0c7f-7cfb-f3d127dc2242@linaro.org>

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)

  parent reply	other threads:[~2020-08-07 18:22 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-08-07 14:06 Luis Machado
2020-08-07 14:56 ` Joel Brobecker
2020-08-07 15:48   ` Jakub Jelinek
2020-08-07 18:21 ` Jonathan Wakely [this message]
2020-08-07 19:09 ` Christian Biesinger
2020-08-11 13:55 ` Nathan Sidwell
2020-08-11 15:48   ` Jonathan Wakely
2020-08-12  2:46   ` Liu Hao
2020-08-12 18:40     ` David Blaikie
2020-08-13  6:44       ` Liu Hao
2020-08-13  8:03         ` Jonathan Wakely

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=CAH6eHdSrmOh47Qf6XxpRmup-AM_Dn_CGo44t-JRXg5z2JF_-1g@mail.gmail.com \
    --to=jwakely.gcc@gmail.com \
    --cc=gcc@gcc.gnu.org \
    --cc=gdb@sourceware.org \
    --cc=luis.machado@linaro.org \
    --cc=palves@redhat.com \
    --cc=simark@simark.ca \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).