[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [groff] placement of const is _not_ a matter of style ...
From: |
Mike Bianchi |
Subject: |
Re: [groff] placement of const is _not_ a matter of style ... |
Date: |
Sat, 5 May 2018 17:31:48 -0400 |
User-agent: |
Mutt/1.5.23 (2014-03-12) |
On Sat, May 05, 2018 at 05:27:39PM +0100, Ralph Corderoy wrote:
> > >> const char *foo;
> > >> char const *foo;
>
> No, those two have identical meaning.
Not according to:
https://en.wikipedia.org/wiki/Const_(computer_programming)
This is a rather through discussion of the topic.
Well worth the read.
These examples are found there.
(( reformatted for emphasis ))
void Foo( int * ptr,
int const * ptrToConst,
int * const constPtr,
int const * const constPtrToConst )
{
*ptr = 0; // OK: modifies the "pointee" data
ptr = NULL; // OK: modifies the pointer
*ptrToConst = 0; // Error! Cannot modify the "pointee" data
ptrToConst = NULL; // OK: modifies the pointer
*constPtr = 0; // OK: modifies the "pointee" data
constPtr = NULL; // Error! Cannot modify the pointer
*constPtrToConst = 0; // Error! Cannot modify the "pointee" data
constPtrToConst = NULL; // Error! Cannot modify the pointer
}
int *ptr; // *ptr is an int value
int const *ptrToConst; // *ptrToConst is a constant (int: integer value)
int * const constPtr; // constPtr is a constant (int *: integer pointer)
int const * const constPtrToConst;
// constPtrToConst is a constant (pointer)
// as is *constPtrToConst (value)
const int *ptrToConst; //identical to: int const *ptrToConst,
const int *const constPtrToConst;
//identical to: int const *const constPtrToConst
Please note that I am not trying to pick a fight here.
A thorough understanding of any programming language, such as C and C++,
is essential to writing code that needs to live through the ages and
be passed along through many hands.
In my experience it is confusions such as these that lead to long-standing
if hard-to-experience bugs.
Seeing the placement of the const keyword as an element of "style" is exactly
the sort of thing that makes me say "I can make this look better" when
in fact I am making it mean something different.
Not fun.
Believe me.
I speak from experience.
An open question is:
Is there a path by which the confusions such as these can be
avoided in the design and implementation of C-like language?
--
Mike Bianchi