J
John Koleszar
Hi all,
I'm porting some code that provides compile-time assertions from one
compiler to another and ran across what I believe to be compliant code
that won't compile using the new compiler. Not naming names here to
remove bias - I'm trying to tell if I'm relying on implementation defined
behavior or if this is a bug in the new compiler.
Consider this stripped down example:
#include <stddef.h>
struct s { int a[3]; int b; };
void foo(void) {
switch(0) {case offsetof(struct s,a[0]):;}
}
The relevant part of the C standard says that:
7.17 Common definitions <stddef.h>
<snip>
3 The macros are
<snip>
offsetof(type, member-designator)
which expands to an integer constant expression that has type size_t,
the value of which is the offset in bytes, to the structure member
(designated by member-designator), from the beginning of its structure
(designated by type). The type and member designator shall be such that
given
static type t;
then the expression &(t.member-designator) evaluates to an address
constant. (If the specified member is a bit-field, the behavior is
undefined.)
It's my feeling that &(t.a[0]) does (can, should) evaluate to an address
constant, as the types are all complete. Other compilers (at least two
different vendors) are able to evaluate this expression as an address
constant. However, the new compiler raises an error 'case expression not
constant.' The new compiler is able to properly evaluate the offsetof a
or b, but not any element of a. What I'd really like is offsetof c.bar
[0].baz, but it seems to choke on any subscripted element. a[0] does work
as part of a non-constant expression though, eg printf(offsetof(a[0]))
So, what say the language lawyers?
br,
John
I'm porting some code that provides compile-time assertions from one
compiler to another and ran across what I believe to be compliant code
that won't compile using the new compiler. Not naming names here to
remove bias - I'm trying to tell if I'm relying on implementation defined
behavior or if this is a bug in the new compiler.
Consider this stripped down example:
#include <stddef.h>
struct s { int a[3]; int b; };
void foo(void) {
switch(0) {case offsetof(struct s,a[0]):;}
}
The relevant part of the C standard says that:
7.17 Common definitions <stddef.h>
<snip>
3 The macros are
<snip>
offsetof(type, member-designator)
which expands to an integer constant expression that has type size_t,
the value of which is the offset in bytes, to the structure member
(designated by member-designator), from the beginning of its structure
(designated by type). The type and member designator shall be such that
given
static type t;
then the expression &(t.member-designator) evaluates to an address
constant. (If the specified member is a bit-field, the behavior is
undefined.)
It's my feeling that &(t.a[0]) does (can, should) evaluate to an address
constant, as the types are all complete. Other compilers (at least two
different vendors) are able to evaluate this expression as an address
constant. However, the new compiler raises an error 'case expression not
constant.' The new compiler is able to properly evaluate the offsetof a
or b, but not any element of a. What I'd really like is offsetof c.bar
[0].baz, but it seems to choke on any subscripted element. a[0] does work
as part of a non-constant expression though, eg printf(offsetof(a[0]))
So, what say the language lawyers?
br,
John