Re: enum + long
- From: Kenneth Brody <kenbrody@xxxxxxxxxxx>
- Date: Mon, 20 Mar 2006 09:52:39 -0500
Laurent Deniau wrote:
Kenneth Brody wrote:
Laurent Deniau wrote:
Eric Sosman wrote:
Laurent Deniau wrote On 03/17/06 12:25,:
[...]
how to define a synonym of a constant address?
enum { p = (ptrdiff_t)&address_of_an_unknown_variable }; // boom,
ptrdiff_t is generally of type long.
When you specify a value for an enum constant, you must use a
compile-time constant expression. Addresses are not compile-time
constants.
An enum requires an integral constant expression and the address of a
variable with static storage duration is constant (6.6-9). But it seems
that the conversion to ptrdiff_t break the constness leading to an
invalid expression (i.e. either not constant, either not integral).
The address of a variable with static storage is a runtime constant.
An enum requires a compile-time constant.
I do not catch this point. The terms "runtime constant" or "compile-time
constant" are not part of the norm. Even if I am aware that the address
is computed during the link phase, that is before program runtime but at
a time where the enum does not exist anymore, from the point of view of
the norm I don't see anything which asserts that an address of a
variable with static storage duration is not constant.
How about this:
extern int foo;
enum { p = (ptrdiff_t)&foo };
char array[p];
BTW a such
address is qualified as a "constant address" in the norm and it can be
use to initialize a constant pointer with static storage duration
(6.6-7). In other terms, if the conversion of the constant address into
an integral type would have been the identity, the compiler could have
been able to use the enum as an alias and replace its use by the
corresponding address in the TU. Unfortunately the conversion is not the
identity (my last remark).
Suppose that converting an address into an integer causes some sort of
conversion. (One such example would be 16-bit segmented architecture
where pointers are 32 bits, but ints are 16 bits.)
extern int foo, bar;
enum { myfoo = (ptrdiff_t)&myfoo,
mybar = (ptrdiff_t)&bar
} myfoobar;
myfoobar foobar;
...
switch(foobar)
{
case myfoo:
break;
case mybar:
break;
}
What happens in the case where &myfoo and &mybar, when converted to an
int, are identical? Given that this cannot be detected at compile time,
and given that you cannot have identical cases, how would you handle
this?
--
+-------------------------+--------------------+-----------------------------+
| Kenneth J. Brody | www.hvcomputer.com | |
| kenbrody/at\spamcop.net | www.fptech.com | #include <std_disclaimer.h> |
+-------------------------+--------------------+-----------------------------+
Don't e-mail me at: <mailto:ThisIsASpamTrap@xxxxxxxxx>
.
- Follow-Ups:
- Re: enum + long
- From: Laurent Deniau
- Re: enum + long
- References:
- enum + long
- From: Laurent Deniau
- Re: enum + long
- From: Eric Sosman
- Re: enum + long
- From: Laurent Deniau
- Re: enum + long
- From: Kenneth Brody
- Re: enum + long
- From: Laurent Deniau
- enum + long
- Prev by Date: Re: Code for ejecting the CD-drive.
- Next by Date: Re: FASTER string copy ????
- Previous by thread: Re: enum + long
- Next by thread: Re: enum + long
- Index(es):
Relevant Pages
|