On 09/04/18 18:25, bartc wrote:
> On 09/04/2018 14:51, Rick C. Hodgin wrote:
>> On 4/9/2018 9:10 AM, bartc wrote:
>>> On 09/04/2018 13:48, Rick C. Hodgin wrote:
>>>> Has there ever been a proposal to the C++ standards committee to have
>>>> something like this:
>>>>
>>>> 01: CWhatever *a, b;
>>>> 02: CWhatever* a, b;
>>>>
>>>> 01 produce a pointer to an instance of CWhatever in a, and it
>>>> allocates an actual CWhatever for b, while the declaration in
>>>> 02 to produce pointers to instances of CWhatever for both a
>>>> and b?
>>>>
>>>
>>> So, C++ syntax isn't complicated or ambiguous enough, it's necessary
>>> to make white space significant?
>>
>> I think both 01 and 02 above producing the same declarations is very
>> confusing. It's burned me a few times when I've looked at code, or
>> gone to create new variables.
>
> This syntax comes from C, which C++ unwisely adopted (I believe in the
> interests of improving take-up of the language which would have been
> harder with an incompatible new syntax).
>
It would have been better if it had never been possible to mix a type
and pointers to the type in a single declaration in C. But since C had
it, C++ had no choice about how to interpret the same code.
What would have been possible in C++ would be to deprecate the mixture -
allowing either "int a, b;" or "int *a, *b;" but giving an error for
"int a, *b;" or "int *a, b;" regardless of white space positioning.
> It's been done like that for coming up to half a century, but according
> to the experts on comp.lang.c, it's not a problem at all provided you
> use the right tools, use the right guidelines (one declaration per line)
> and learn C properly so you that will know that the example declares one
> pointer only.
>
People make mistakes with this sort of thing, and it would have been
better (IMHO) if mixing types and pointers in one declaration had never
been allowed in C (or C++). But it is not a /problem/ - if you don't
make such mixes, you don't get it wrong. And if you /do/ get it wrong,
you will almost certainly get an error somewhere in the code when you
use the variables.
/Problems/ come when there are mistakes you can easily make that are
hard to spot, hard to avoid despite good coding rules, and which are not
easily detected automatically by tools. There are plenty of /real/
problem areas in C - you don't need to invent them out of things that
are simply not as nice as they could have been.
> But as you say, you can do all that and still get tripped up. It could
> even, if you've mistyped and CWhatever is the name of a variable rather
> than a type, end up writing an expression that multiplies CWhatever by
> a, then yields the value b.
First, however, you will have had an expression that assigns a value to
a or b - that will always come before the use of the variable. And that
assignment is generally going to be a syntax error unless you are
over-zealous with casting.
There will be occasions where both T and T* types are valid, but they
are rare.
>
>> My C-like language operates that way.
>
> If this is a new language, why repeat the mistake of C++ and try and
> have compatible syntax? Do something obviously new rather than
> perpetuating design errors from 50 years ago.
Agreed.
>
> If you need compatibility, do something like this:
>
> CWhatever *a, b; // old rules, one pointer
> var CWhatever *a, b; // new rules, two pointers
>
Disagreed.
> and use the new form until the old can be deprecated. Then you can drop
> the 'var'. And not need to rely on white space to alter meaning.
>
Strongly disagreed - making a significant change in the interpretation
would be crazy.
Try this:
CWhatever *a, b;
-> Warning - mixing types and pointers is deprecated in "C mode"
-> Error - mixing types and pointers is not allowed in "CAlive mode"
The alternative is to use a noticeably different syntax to avoid the
possibility of errors or confusion. For example:
var CWhatever : a, b; // not pointers
var CWhatever* : a, b; // both pointers
var CWhatevr : *a, b; // syntax error