Topic: Initializer w/sizeof() on lhs term, okay or not?


Author: tottinge@csci.csc.com (Tim Ottinger)
Date: 1996/02/14
Raw View
I had inherited someone else's (generally excellent) code, and found a few
quirks (via CC).  Only one is trivial totally amusing to me.  The rest were
also trivial and less amusing.

In the following example, the problem is manifest nicely, but the actaul 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);
                 ^                  ^^^^^^^^

and all is well.

So, is the compiler right on this one, that it's certainly disallowed, or is
it merely eccentric?

Tim Ottinger
Sr. Tech
Computer Sciences Corp

******************************************************************************
* Tim Ottinger, Sr Tech           tottinge@csci.csc.com *
* CSC Communications Industry Services             217-351-8508x2420 *
* TRIS Division -- Cellular Billing and Support             Fax 217-351-2640 *
******************************************************************************
* Requirements have always been the least stable part of any project I have  *
* worked upon.                         -- Robert Martin via Usenet 12/02/95 *
******************************************************************************
---
[ 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. ]