Request for compiler option to disable multiple declarations in a single statement
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 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. 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. Would my idea have takers on this list ? -- Thank you & Regards, Manish Jain
Re: Request for compiler option to disable multiple declarations in a single statement
On 04/19/18 14:46, David Brown wrote: > Certainly it is heavily used in existing code - making an option > to disable it would be impractical. Thanks for replying, Mr. Brown. What I meant was if an option could be provided, existing code could compile without the option, and fresh code to compile with the switch enabled (as per user discretion, of course - no compulsion either way anywhere). If the option garners wide recognition / usage, over a period of time the standard pointer declaration could be fixed to everyone's happiness: int* p; // which really is what it should be in all propriety The current declaration style really sickens me, particularly when trying to explain C pointers to others. Manish Jain
Re: Request for compiler option to disable multiple declarations in a single statement
> Wars have been fought over less. I joined the list to make the request. So I just hope my war is not in a lonely one-man army. >> 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. Fine - if anyone could create a warning specifically for multiple declarations (and which can be turned into Werror such that it breaks code for multiple declarations -- and absolutely nothing else), that serves the purpose as well as an option like --single-declarations. I don't even mind if programmers have to use pragma directives. The end result I want is the programmer to have some way of getting the compiler break code rather than compile the code at all. Right now, it is a real mess that just builds on a simple oversight on the part of Dennis Ritchie decades back. The most maddening way to address that mess is drop the space between the asterisk and the pointer variable in a declaration. Every single thing about pointer usage goes downhill from that clumsy fix. > 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) > 'idiomatic C++' looks more like 'idiotic C++' ever since v11/v14 came out. Does Stroustroupe have nothing better to do these days than to make a mockery of the once acceptable language ? I like C++ these days just as a seafarer likes a close encounter with a shark.