Re: [Long, historical] Re: Var* b or Var *b

From: Denis Remezov (REMOVETHISdenis_remezov_at_yahoo.removethis.ca)
Date: 06/02/04


Date: Wed, 02 Jun 2004 02:26:59 +0000

David White wrote:
>
> "Jeff Schwab" <jeffplus@comcast.net> wrote in message
> news:2_-dnUuqS7UcZiHdRVn-tw@comcast.com...
> > Arthur J. O'Dwyer wrote:
> >
> > ...an interesting proposed explanation for common styles of declaration
> > in C and C++.
> >
> > Thanks, that's an interesting take. I'm afraid I missed the Algol
> > years, but your point that all-on-one-line declaration syntax is
> > certainly used to save space in technical publications, and may well be
> > the biggest reason I see it used so much in practice. I don't work for
> > a commercial software company, though, so perhaps C coding styles in
> > industry are more like C++ (separate line for each declaration) than I
> > realized.
> >
> > For whatever reason, when I'm reading C++, I like to see pointer
> > declarations with the asterisk next to the type name, rather than the
> > variable; i.e.:
> >
> > Type* variable;
> >
> > Rather than:
> >
> > Type *variable;
> >
> > What's being declared is of type pointer-to-type, and I think of the
> > "pointiness" (sorry) as a property of the type, rather than the
> > variable.
>
> It's just an accident of history that the pointer syntax is such that you
> can move the * over to the type name. The * could have been put on the
> right. The "Type* variable" style:
> - contradicts the language's grammar rules
> - can only be used for some types
> - confuses the hell out of those new to the language, who have enough
> trouble with the declaration syntax already
>
> For the third reason, I suggest that all declarations in this style come
> with a commented warning. Another idea is a compiler warning, so that when
> newbies compile the code they'll know that the style is in defiance of the
> language's grammar.
>
> DW

I think we all can get religious about it if we choose to.

I, for one, was about to type the reverse of your statements above.
I think of Type *var; mainly as of an unfortunate accident of history.

To me,
  Type* variable;
is much more consistent both in terms of bits and grammar.

0. Bits:
You define an entity which is a /pointer/, a real thing of bits and bytes,
not something abstract of type "Type" with a magic thingy '*'. This pointer
does not even need to be used with an '*'. You can end up only using ->
with it, for example.

1. Grammar:
Consider this:
  typedef T* T_ptr;

...and the following two definitions:
  T* p;
  T_ptr p; //look, no '*'!

They are equivalent, and they do look equivalent. However,
  T *p;
is just inconsistent with the second definition above and the spirit
of the C++ type system.

Further, consider this:

  T* const p;
  T_ptr const p; //same thing; don't need any '*' ('*' is a fluke)

Now do that in your style:
  T *const p;
Where did the '*' go? Can you put it next to the variable name any
longer?

To the OP:

  Type* variable;

is the right way. (as I said, this is a religious topic).

Denis



Relevant Pages

  • Re: confused constructing a regex
    ... Can declarations be broken across lines? ... That looks pretty Formal (as in Formal Methods). ... then find the grammar for it. ... anonymous array elements. ...
    (comp.lang.perl.misc)
  • Re: K&R2 variable definition
    ... Clausfor said: ... beginning of a compound statement. ... Now look for other places in the grammar ... where declarations *may* occur, and look at... ...
    (comp.lang.c)
  • Re: Am I a crank?
    ... even when grammatically sentences, are not declarations, and only ... delarations need be either true or false. ... If you can't figure that out for yourself, sport, you are too dim to ... Whereas you prefer to comment on grammar instead. ...
    (sci.math)