public inbox for gcc@gcc.gnu.org
 help / color / mirror / Atom feed
From: Jonathan Wakely <jwakely.gcc@gmail.com>
To: Jay K <jayk123@hotmail.com>
Cc: gcc <gcc@gcc.gnu.org>
Subject: Re: extern const initialized warns in C
Date: Mon, 22 Jan 2018 15:19:00 -0000	[thread overview]
Message-ID: <CAH6eHdQ+42VO14YtZrYpzC4rz+0_C37L7RfsEXf1gBALqRGCyg@mail.gmail.com> (raw)
In-Reply-To: <MWHPR18MB121457D8901E94B1F0F339D6E6ED0@MWHPR18MB1214.namprd18.prod.outlook.com>

On 21 January 2018 at 07:12, Jay K wrote:
> extern const int foo = 123;
>
>
>
> Why does this warn?
> This is a valid portable form, with the same meaning
> across all compilers, and, importantly, portably
> to C and C++.
>
> I explicitly do not want to say:
>
>   const int foo = 123
>
> because I want the code to be valid and have the same meaning
> in C and C++ (modulo name mangling).
>
> I end up with:
>
> // Workaround gcc warning.
> #ifdef __cplusplus
> #define EXTERN_CONST extern const
> #else
> #define EXTERN_CONST const
> #endif
>
>
> EXTERN_CONST int foo = 123;
>
> and having to explain it to people.

Why not simply:

extern const int foo;
const int foo = 123;

without the ugly macro?

Or, according to taste:

// Without an extern declaration, file-scope const variables are static:
extern const int foo;
const int foo = 123;

Or:

#ifdef __cplusplus
// Without an extern declaration, file-scope const variables are static:
extern const int foo;
#endif
const int foo = 123;

But your macro version is just ugly. And instead of complaining that
you have to explain it to people, why not explain it with a comment in
the code? That's what comments are for.

      parent reply	other threads:[~2018-01-22 15:19 UTC|newest]

Thread overview: 18+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <MWHPR18MB121493D9F2292547E0AF6C3AE6ED0@MWHPR18MB1214.namprd18.prod.outlook.com>
2018-01-21  7:12 ` Jay K
2018-01-21 12:08   ` Georg-Johann Lay
2018-01-22  9:44     ` Franz Sirl
2018-01-22 15:20     ` Jonathan Wakely
2018-01-25 12:27       ` Georg-Johann Lay
2018-01-25 12:29         ` Jonathan Wakely
2018-01-25 12:30           ` Jonathan Wakely
2018-01-22  8:32   ` David Brown
2018-01-22  9:32     ` Jay K
2018-01-22  9:39       ` Jay K
2018-01-22 10:14         ` David Brown
2018-01-22 10:25           ` Jay K
2018-01-22  9:54       ` David Brown
     [not found]         ` <MWHPR18MB12145B5B3E0B093993C2D214E6EC0@MWHPR18MB1214.namprd18.prod.outlook.com>
2018-01-22 10:42           ` David Brown
2018-01-22 11:27             ` Jay K
2018-01-22 13:02               ` David Brown
2018-01-22 16:22         ` Vincent Lefevre
2018-01-22 15:19   ` Jonathan Wakely [this message]

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=CAH6eHdQ+42VO14YtZrYpzC4rz+0_C37L7RfsEXf1gBALqRGCyg@mail.gmail.com \
    --to=jwakely.gcc@gmail.com \
    --cc=gcc@gcc.gnu.org \
    --cc=jayk123@hotmail.com \
    /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).