Re: enum + long



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>


.



Relevant Pages

  • Re: Property accessors "add" and "remove" with event properties and EventHandlerList
    ... don't see why event delcarations need to be available at compile-time ... collection with some sort of enum key - but then you'd need to ... the Observer pattern and the Chain of Command pattern. ... extent of that idiom is. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Binding a switch to an Enum at compile time?
    ... Adam Blair wrote: ... > Is it possible to bind a switch statement to an Enum such that a compile-time ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Design Pattern (maybe)
    ... If this is four options that are known at compile-time, then switch ... make a factory method that accepts an enum and returns the common- ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Can enum be added to dynamically
    ... >> Can an enum start empty and be added to on the fly? ... A receives a message from B containing an enum value it wasn't compiled ... wasn't present at compilation time, ... it turns out to be handy to be able to distinguish compile-time from ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Safe conversion from floating real to integer (and vice versa)
    ... >> Given a value of floating real type Tf, unknown at compile-time. ... I just meant that it would be a hypothetical situation where the compiler cannot predict in any way the value and thus the conversion has to be done at run-time with no warning/error/diagnostic given at compile-time. ... "When a value of integer type is converted to a real floating type, if the value being converted can be represented exactly in the new type, it is unchanged. ...
    (comp.lang.c)