This is the mail archive of the
mailing list for the GCC project.
Re: Request for compiler option to disable multiple declarations in a single statement
- From: Jonathan Wakely <jwakely dot gcc at gmail dot com>
- To: Manish Jain <bourne dot identity at hotmail dot com>
- Cc: "gcc at gcc dot gnu dot org" <gcc at gcc dot gnu dot org>
- Date: Thu, 19 Apr 2018 10:21:59 +0100
- Subject: Re: Request for compiler option to disable multiple declarations in a single statement
- References: <VI1PR02MB120069566F352087BFB27930F6B50@VI1PR02MB1200.eurprd02.prod.outlook.com>
On 19 April 2018 at 09:09, Manish Jain wrote:
> Hi all,
> One of the historical artefacts of the C language has been the burden of
> lugging around multiple declarations in a single statement, with some
> well-known pitfalls:
> int* ptr1, ptr2;
> Since ptr2 looks like a pointer but actually is not, standard coding
> guidelines recommend declaring like this:
> int *p1, *p2;
> If anything, this leads to bizarre statements - very misleading for
> those trying to understand pointer usage in C or just read code:
> int i;
> int *j = &i; // impression: *j is being assigned &i
> char *k = "Text"; // impression: *k is "Text"
> void *fx(char *z); // impression: *fx is will accept char & return void
I don't think these are such common misconceptions that a new compiler
flag would help, especially since that new flag would not be the
default so most beginners would not use it.
> Each of these idiosyncrasies is best avoided by retaining the space
> after the asterisk (and removing the one before) in a pointer
> declaration. This really ought to be the standard coding guideline.
Wars have been fought over less.
> As for the problem of multiple declarations fraught in the suggestion
> above, I would like gcc developers to please consider a compiler option
> (--single-declarations perhaps) under which the programmer can only
> introduce one declaration in one statement. If such an option could be
> made available, it takes care of all declaration woes and lets declared
> types bear close resemblance to what they appear to be from signatures.
It might be appropriate as a warning option, for those who choose to
enforce that style.
And of course there are cases where avoiding multiple declarations
changes the meaning of the code, such as this idiomatic C++:
for (auto first = c.begin(), last = c.end(); first != last; ++first)