<<< Date Index >>>     <<< Thread Index >>>

const correctness



I read the articles on const correctness, and while I see the authors
point (regarding the order), it seems to me it's just a matter of how
one looks at it.

I read left to right, as most of us do...  The traditional order of
variable declarations, as we have established, is this:

  const int *i;

Interpreting this works just like natural language, when read left to
right -- specifically it works like nouns with adjective modifiers.
In this case, "pointer-i" is the noun, whereas "const" and "int" are
adjectives, which describe pointer-i. 

It works equally well with the other cases:

  int * const i;

Except in this case, there are 2 nouns... pointer, and i.  The
adjective "int" can be said to modify pointer, whereas  the adjective
"const" modifies i.  The two nouns can be thought of as appositives...
More on that below.

Returning to the first case, you can change the order:

  int const *i;

This conveys the same meaning, but strikes the reader as strange.  The
reason it is grammatically wrong in natural language.  Here, in
natural language, int (short for integer) is actually a noun, being
used to modify another noun (*i).  We do this all the time in natural
language:

  butcher knife
  tear gas
  horse saddle

But when we use this construction, real adjectives must precede the
noun being used as a modifier:

  large butcher knife
  old tear gass
  worn horse saddle

In the case of declaring variables, it is being used as an
adjective... to declare a constant pointer, or a constant integer.  It
should precede the type identifier.

The only confusing part is that in natural language, "constant" is
used as both a noun and an adjective...  When it is an adjective, it
is a pure adjective, not a nound being used as an adjective.  This
point is subtle, and can easily be confusing, especially to non-native
speakers.  In other words, if you look up "horse" in a dictionary, it
will only be listed as a noun, though sometimes we may use it to
modify other nouns.  If you look up "constant" in a dictionary, it
will be listed as both a noun and an adjective, as its use is dual,
and subtlely different syntactically.

Confusion arises with this (grammatically) unfortunate selection of
keywords, because you could, for example, write this:

  integer constant pointer-i

But here, constant is a noun, and really pointer-i is an appositive
for constant; it should technically be written "integer constant,
pointer-i." However, years of lazy language usage have seen the comma
degraded.  The construction is the same as, "John, my brother's
friend, is coming to the party."  Here, "my brother's friend" is an
appositive for John.  It is syntactically the same as "integer
constant, pointer-i."  Most people would be inclined to leave off the
comma in the latter case, however; just as we would do if we reversed
the order of the other example, to "my brother's friend John."  It is
still technically correct to include the comma: "my brother's friend,
John."  Likewise, we can say, "integer pointer, constant i (equivalent
to int * const i)."

But I digress...  =8^)

My point is, if you have a clear understanding of English grammar, the
meaning is not confused if you use natural language to read the
keywords in natural language order.  You simply need to understand the
natural grammar, and understand what programmatical elements are being
used as a noun equivalent.  The meaning is described precisely, in
natural order, and without having to insert a whole lot of other
verbiage to be understood, as is necessary when reading from right to
left according to the article author's point of view.  


-- 
Derek D. Martin    http://www.pizzashack.org/   GPG Key ID: 0xDFBEAD02
-=-=-=-=-
This message is posted from an invalid address.  Replying to it will result in
undeliverable mail.  Sorry for the inconvenience.  Thank the spammers.

Attachment: pgpwVnXGxNHQT.pgp
Description: PGP signature