Topic: Why is overloading sizeof() not allowed?


Author: bom@slip.net (Ravikant Iyer)
Date: 1996/10/25
Raw View
As a side note I tried to look up the April 95 DWP and under Overloaded
Operators 13.5 [over.oper] , point 1 shows operators that may be overloaded
and point no 3 lists those that cannot,

none of them mention sizeof!!

may be a typo.. may be it is allowed to be overloaded, can someone clarify?

thanks
-bom
---
[ comp.std.c++ is moderated.  To submit articles: Try just posting with your
                newsreader.  If that fails, use mailto:std-c++@ncar.ucar.edu
  comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
  Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
  Comments? mailto:std-c++-request@ncar.ucar.edu
]





Author: "Daniel Pflager" <dpflag@ix.netcom.com>
Date: 1996/10/15
Raw View
Or maybe it is now?

I've implemented several large class libraries where an integral part of
the functionality involved marshalling (flattening). Almost all the classes
could be had to have a byte format which was predefined, so we had to
define flattening/unflattening functions. Along with flatten(), and
unflatten() we defined flatsizeof(). What we would have liked to do is
define an "operator byte*()", define a constructor taking byte*, and
overload the sizeof() operator to provide the size of the flattened object.

Does anyone know why not?

Daniel
---
[ comp.std.c++ is moderated.  To submit articles: Try just posting with your
                newsreader.  If that fails, use mailto:std-c++@ncar.ucar.edu
  comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
  Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
  Comments? mailto:std-c++-request@ncar.ucar.edu
]





Author: clamage@taumet.eng.sun.com (Steve Clamage)
Date: 1996/10/15
Raw View
In article 31d8cac6@chico, "Daniel Pflager" <dpflag@ix.netcom.com> writes:
>Or maybe it is now?
>
>I've implemented several large class libraries where an integral part of
>the functionality involved marshalling (flattening). Almost all the classes
>could be had to have a byte format which was predefined, so we had to
>define flattening/unflattening functions. Along with flatten(), and
>unflatten() we defined flatsizeof(). What we would have liked to do is
>define an "operator byte*()", define a constructor taking byte*, and
>overload the sizeof() operator to provide the size of the flattened object.
>
>Does anyone know why not?

Aside from syntactic peculiarities (accepts a type or an expression as an
argument), "sizeof" reports the size used by the implementation in allocating
space for the argument's type. Overloading that operator sounds like a
poor idea to me, since "sizeof" would cease to have its previoius meaning for
classes. In addition, what should happen in this case:
 class Myclass {
 public:
  operator sizeof(); // implicitly static, like "operator new"
  ...
 } M;
 char  foo1[sizeof(Myclass)];      // static allocation
 char* foo2 = new char[sizeof(M)]; // runtime allocation

Either "sizeof" ceases to be a constant, or foo1 and (*foo2) have different
sizes. Neither choice seems very attractive to me, if the only benefit is
to be able to overload sizeof.

Why is writing "flatsizeof" unacceptable compared to writing "sizeof"?
It is clear with "flatsizeof" that you are finding out something special
about the type.  If you used "sizeof", the code would be misleading.
I find the word "flatsizeof" fairly unattractive, but you could use "size",
or "mysize", for example; either would convey the appropriate meaning.
---
Steve Clamage, stephen.clamage@eng.sun.com




[ comp.std.c++ is moderated.  To submit articles: try just posting with      ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu         ]
[ FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html    ]
[ Policy:   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu                             ]