Topic: sizeof struct data member


Author: rfg@netcom.com (Ronald F. Guilmette)
Date: Mon, 18 Apr 1994 10:22:19 GMT
Raw View
In article <1994Apr13.013430.24202@cs.brown.edu> kr@cs.brown.edu (Kevin Regan) writes:
>Is the following legal in c++?
>
>struct A {
>   int b;
>}
>
>int c = sizeof(A::b);
>
>I have found a difference of opinion (among people as well
>as compilers) as to the validity of the previous snippet of code.
>
>sizeof takes an expression and by the C++ syntax, A::b is an
>expression.

That may indeed have the symtactic form of an expression, but at least
one committee member has claimed (in the past) that it is a special kind
of an expression which simply has no type.

I found that suggestion rather humorous at the time... and I still do.
If the committee does indeed decide NOT to specify a type for such things,
then such things will be very peculiar kinds of expression indeed!

>However, some compilers I have used seem to want
>b to be accessed through an object and will not compile the above
>code while others compile it without a complaint.

You sound surprized.  Compilers will not agree until there is a clear,
complete, and self-consistant standard (at the earliest).

--

-- Ron Guilmette, Sunnyvale, CA ---------- RG Consulting -------------------
---- domain addr: rfg@netcom.com ----------- Purveyors of Compiler Test ----
---- uucp addr: ...!uunet!netcom!rfg ------- Suites and Bullet-Proof Shoes -




Author: kr@cs.brown.edu (Kevin Regan)
Date: Wed, 13 Apr 1994 01:34:30 GMT
Raw View
Is the following legal in c++?

struct A {
   int b;
}

int c = sizeof(A::b);

I have found a difference of opinion (among people as well
as compilers) as to the validity of the previous snippet of code.

sizeof takes an expression and by the C++ syntax, A::b is an
expression.  However, some compilers I have used seem to want
b to be accessed through an object and will not compile the above
code while others compile it without a complaint.

The ARM states (pg 48, paragraph 2):

   A qualified-class-name (7.16) followed by :: and the name of
a member of that class (9.2), or a member of a base class (10), is
a qualified-name; its type is the type of the member.

I think this says that the above code is legal.  Does anyone
have a definite answer about this?

--
Kevin Regan

kr@cs.brown.edu
Brown University
Providence, RI
02912