comp.lang.ada
 help / color / mirror / Atom feed
From: David Thompson <dave.thompson2@verizon.net>
Subject: Re: Ada Smileys in C++ lib Conversion
Date: Tue, 24 Aug 2010 06:12:04 -0400
Date: 2010-08-24T06:12:04-04:00	[thread overview]
Message-ID: <1l477696k7ss5d0pvkrmqk8hgf8ts7e85v@4ax.com> (raw)
In-Reply-To: a5fe665sg9vhdh80d5p8cqu6v3dlpfk5to@4ax.com

On Sun, 15 Aug 2010 02:16:15 +0100, Brian Drummond
<brian_drummond@btconnect.com> wrote:

> On Sat, 14 Aug 2010 13:34:54 -0700 (PDT), Maciej Sobczak
> <see.my.homepage@gmail.com> wrote:
<snip: C const int>
> >> Of course I was declaring integer variables, who's storage class happens to be
> >> named "constant".
> >No, there is no such storage class. If these objects were globals
> >(which was most likely the case), then their storage class is static.
> 
> OK you're right, it's a type qualifier not a storage class.
> 
> >Const means that the given name cannot be used to modify the referred
> >object and has nothing to do with the storage class (it might
> >influence the linkage, though).
> 
Depends which linkage. 'linkage' is a term of art in the C standard,
with external or 'global globals' visible throughout the program vs
internal or 'file globals' visible only in one (preprocesed) source
file and declared with the misleading keyword 'static'.

C++ adds namespaces (~packages) which can hierarchically organize
external names. And it makes const objects (~variables) (and
references) default to internal linkage vs external in C (except
locals have no linkage). (C++ also adds a separate but similar
'language linkage' for interop with C and potentially others.)

And as already noted, in C++ but not C a const object of integer or
enumeration type initialized by a constant expression is usable as a
compile-time constant (where a constant expression is required,
including array bound, case label, etc.) 

If you mean linkage as the normal compiler+linker+run process, yes
static duration objects that are const qualified can be put in a
separate section (or equivalent depending on platform) that is
readonly at runtime e.g. .RODATA instead of .DATA .

> However, it does NOT mean the referred object cannot be modifed.
> Nor does it mean that you can use the object just anywhere you'd 
> expect to use a constant.
> 
An object (~variable) _definition_ in C (a declaration that also
allocates the storage) with qualifier 'const' means that a store
through that name (or other const 'lvalue' ~view) is a 'Constraint
Violation' and must be diagnosed, but if you 'cheat' and get a
nonconst lvalue, any store to it is 'Undefined Behavior' according to
the standard. U.B. is like Ada erroneous -- a program mustn't do it,
but an implementation isn't required to diagnose it.

In practice usually either the object is actually writable and the
write works, or it is allocated to readonly address-space (as above)
and the write either traps or is silently ignored. But the compiler is
allowed to assume no such erroneous write occurs, and fold the
(visible) declared initialization value to any read.

And other declarations that _refer_ to the object must be 'compatible'
according to specific rules, and const T and nonconst T aren't. Again
in practice, this will usually work in the sense of accessing the
correct memory location, and writes using that name either will or
won't work, but officially this also is U.B.

Pointer targets are different. If I declare 'const int * p' I can
point p to either a const or nonconst int object, and I can't use
_that name_ i.e. *p= (or p[i]= if it's actually in an array) to store
to it, but if the actual object is nonconst other pointers/name 
can legally modify it, including a cast of p to nonconst int *. 
And similarly for references in C++, which also adds a specific 
syntax const_cast<T> to do only dequalification (actually both const
and volatile, but volatile is probably <<1e-6 of the uses).

And yes, as above, in C++ but not C you can use a const int/enum
object with constant initializer as a constant expression.

> >> But it certainly puzzled me to be told that I couldn't use a "const" in a
> >> constant expression!
> >
> >And what would that mean, anyway? How would you want to use it and
> >what would you like to achieve with it?
> 
> In this instance I wanted to declare an array whose size was the constant
> expression in question.
> 
Yes. You can get this particular case in C (and C++ if you wish) by
declaring a constant in an enum type (which needn't be otherwise
used), but (1) it looks confusing (how does this value relate to this
enum?) and (2) it's limited to (signed) 'int' in C ('long' in C++);
that's _often_ enough for any supported array bound, but not always.




  parent reply	other threads:[~2010-08-24 10:12 UTC|newest]

Thread overview: 25+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2010-08-13 14:20 Ada Smileys in C++ lib Conversion Warren
2010-08-13 15:06 ` Yannick Duchêne (Hibou57)
2010-08-13 16:53 ` Anh Vo
2010-08-14  1:36   ` Warren
2010-08-14  9:52     ` Georg Bauhaus
2010-08-14 10:57     ` Brian Drummond
2010-08-14 20:34       ` Maciej Sobczak
2010-08-14 21:01         ` Dmitry A. Kazakov
2010-08-15  1:16         ` Brian Drummond
2010-08-15 10:04           ` Maciej Sobczak
2010-08-16 12:08             ` Brian Drummond
2010-08-16 12:13               ` Georg Bauhaus
2010-08-16 12:30             ` Brian Drummond
2010-08-16 12:39               ` Maciej Sobczak
2010-08-16 21:39                 ` Brian Drummond
2010-08-16 22:53                   ` Robert A Duff
2010-08-24 10:12           ` David Thompson [this message]
2010-08-30  9:51             ` Brian Drummond
2010-08-16 14:54       ` Warren
2010-08-16 20:27         ` Maciej Sobczak
2010-08-17 12:57           ` Warren
2010-08-17 13:37             ` Maciej Sobczak
2010-08-17 15:07               ` Georg Bauhaus
2010-08-18 17:31               ` Warren
2010-08-24 10:12             ` David Thompson
replies disabled

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox