Topic: Initializer w/sizeof() on lhs term, okay o
Author: clamage@Eng.Sun.COM (Steve Clamage)
Date: 1996/02/15 Raw View
In article qcr@venus.roc.csci.csc.com, tottinge@csci.csc.com (Tim
Ottinger) writes:
>In the following example, the problem is manifest nicely, but the actual
>code fragment is clearly nonsensical. Forgive this. I didn't want to go
>chasing down the code again.
>It all comes down to a problem where a sizeof() is used on the rhs of an
>initialization, and it's operating on the object it's initializing:
>
> someType z = fred + sizeof(z);
> ^ ^^
>Of course, I've changed the inherited code to read:
>
> someType z = fred + sizeof(someType);
> ^ ^^^^^^^^
The original code is valid as shown. The point of declaration of a name is
immediately after its complete declarator and before its initializer (if any),
with a few exceptions that don't apply to this particular example.
Thus, 'z' has been declared by the time you get to the '=', so you can
use 'sizeof(z)' in its initializer. There is no ambiguity, since the size
of z is the size of 'someType', no matter what the value of the initializer.
---
Steve Clamage, stephen.clamage@eng.sun.com
---
[ comp.std.c++ is moderated. Submission address: std-c++@ncar.ucar.edu.
Contact address: std-c++-request@ncar.ucar.edu. Moderation policy:
http://reality.sgi.com/employees/austern_mti/std-c++/policy.html. ]