rfg@riunite.ACA.MCC.COM (Ron Guilmette) (03/20/89)
I've already asked some GNU/C++ people about this issue (including
Michael Tiemann) but I would like to address the question to a
broader audience.
I want to create smart objects which can tell me how big they are.
Specifically, it would be nice to be able to say:
size = sizeof *base_object_pointer;
Where base_object_pointer is a pointer to an object either of a base
class or some derived class. Obviously, in order to make sizeof
return a "correct" value in such cases, the sizeof operator itself
would have to be a an explicitly (re-)defined virtual operator for
both the base class and for all classes derived from that base class.
There are two problems with this. First, neither g++ (1.34.0) nor
cfront 1.2 allow "operator sizeof" to be declared. Second, the ideal
thing would be to have the compiler automagically provide the (trivial)
"function body" for the "virtual operator sizeof" for each class which
needed it. That would save a lot of typing.
I guess that my question boils down to this. Why is the following
(possibly useful) declaration illegal? Must it always be so?
----------------------------------------------------------------------
class sized {
public:
virtual operator sizeof ();
};
----------------------------------------------------------------------
One final point. It seems to me that one of the basic ideas of object
oriented programming is that the programmer should always be able to
specify, for any given "type", the set of legal operations for that type.
It also seems that "sizeof" breaks this rule, because, for any given "type",
the "sizeof" operator is automagically defined, and you cannot either
redefine it or undefine it.
Perhaps that would not be so bad, if only the sizeof operator always yielded
a "correct" value, but in the base/derived example above, "sizeof" can easily
be mislead by the "static" type of the object (rather than yielding the
sizeof value associated with the "dynamic" type of the object).
--
// Ron Guilmette - MCC - Experimental (parallel) Systems Kit Project
// 3500 West Balcones Center Drive, Austin, TX 78759 - (512)338-3740
// ARPA: rfg@mcc.com
// UUCP: {rutgers,uunet,gatech,ames,pyramid}!cs.utexas.edu!pp!rfg
bph@buengc.BU.EDU (Blair P. Houghton) (03/21/89)
In article <123@riunite.ACA.MCC.COM> rfg@riunite.UUCP (Ron Guilmette) writes:
["why isn't sizeof declarable as an operator?"]
Because it's a compiler directive, not a C-language operator. The
result of sizeof (something) is an integer _constant_. If you try to
take the size of something that has a varying size, you will get only
the size of that thing at compile time.
If you're creating varying-sized data elements, you should keep track of
their sizes yourself.
--Blair
"But I don't know why you would..."
turner@sdti.SDTI.COM (Prescott K. Turner) (03/21/89)
In article <2352@buengc.BU.EDU> bph@buengc.bu.edu (Blair P. Houghton) writes: >In article <123@riunite.ACA.MCC.COM> rfg@riunite.UUCP (Ron Guilmette) writes: >["why isn't sizeof declarable as an operator?"] > >Because it's a compiler directive, not a C-language operator. What difference does that make? Note that "sizeof" is in the table of operators in "The C++ Programming Language". >The result of sizeof (something) is an integer _constant_. That was right for C. >If you try to take the size of something that has a varying size, >you will get only the size of that thing at compile time. If "base" is a class with virtual functions, then a pointer declared like base * base_object_pointer; provides the behavior of the derived class. For *base_object_pointer, "the size of that thing" is not available "at compile time", so the compiler's result can be plain wrong. If the program does pointer arithmetic with base_object_pointer, cfront assumes that the declared type of the array elements is "base" -- an assumption which is dangerous most of the time. >If you're creating varying-sized data elements, you should keep track of >their sizes yourself. This is not satisfactory because sizeof and pointer arithmetic are in the language and are considerably simpler to use than known substitutes -- and there's no way to make them work right when you need it. -- Prescott K. Turner, Jr. Software Development Technologies, Inc. 375 Dutton Rd., Sudbury, MA 01776 USA (508) 443-5779 UUCP: ...{harvard,mit-eddie}!sdti!turner Internet: turner@sdti.sdti.com
gnulists@WHEATIES.AI.MIT.EDU (03/28/89)
Return-Path: <MAILER-DAEMON@eddie.mit.edu> Date: Thu, 23 Mar 89 19:56:57 EST From: MAILER-DAEMON@eddie.mit.edu (Mail Delivery Subsystem) Subject: Returned mail: unknown mailer error 1 To: <gnulists@wheaties.ai.mit.edu> ----- Transcript of session follows ----- inews: Cannot get user's name 554 "|/usr/local/lib/news/nrecnews -n comp.emacs -x mirror"... unknown mailer error 1 ----- Unsent message follows ----- Received: by EDDIE.MIT.EDU with sendmail-5.45/4.7 id <AA06792@EDDIE.MIT.EDU>; Thu, 23 Mar 89 19:56:57 EST Received: from wheat-chex.ai.mit.edu by life.ai.mit.edu; Thu, 23 Mar 89 19:57:31 EST Received: from localhost by wheat-chex.ai.mit.edu; Thu, 23 Mar 89 19:57:29 EST Date: Thu, 23 Mar 89 19:57:29 EST From: gnulists@wheaties.ai.mit.edu Message-Id: <8903240057.AA15865@wheat-chex.ai.mit.edu> To: post-net-emacs@eddie.mit.edu Subject: Re: Isn't sizeof an operator? In article <8903202240.AA24954@yahi> tiemann@lurch.stanford.edu writes: > > In article <123@riunite.ACA.MCC.COM> rfg@riunite.UUCP (Ron Guilmette) writes: > ["why isn't sizeof declarable as an operator?"] > > Because it's a compiler directive, not a C-language operator. The > result of sizeof (something) is an integer _constant_. If you try to > take the size of something that has a varying size, you will get only > the size of that thing at compile time. > > If you're creating varying-sized data elements, you should keep track of > their sizes yourself. > > --Blair > "But I don't know why you would..." > >It also takes a TYPE as an argument instead of an EXPRESSION. > >Michael You are 100% incorrect. Remember that the topic of discussion here is C++, not C! sizeof is certainly an operator. And it can take a type or an expression as an argument. See Stroustrup, pp. 257-258. It is, however, resolved at compile time. Presumably, it can't be overloaded for this very reason; it is syntactically an operator, but it is not treated the same as other operators. It may be overloadable in the future; C++ is still an evolving language, and recently -> was added to the list of overloadable operators. However, it would be much nicer if it used the dynamic rather than static information where appropriate, like class/structure element dereferencing!! Are you listening, Bjarne??? Unfortunately, for now, there seems to be no alternative but to keep track of the sizes yourself. Not very helpful, I know, but at least correct. :-) Bob Hearn hearn@claris.com
gnulists@WHEATIES.AI.MIT.EDU (03/28/89)
In article <8903202240.AA24954@yahi> tiemann@lurch.stanford.edu writes: > > In article <123@riunite.ACA.MCC.COM> rfg@riunite.UUCP (Ron Guilmette) writes: > ["why isn't sizeof declarable as an operator?"] > > Because it's a compiler directive, not a C-language operator. The > result of sizeof (something) is an integer _constant_. If you try to > take the size of something that has a varying size, you will get only > the size of that thing at compile time. > > If you're creating varying-sized data elements, you should keep track of > their sizes yourself. > > --Blair > "But I don't know why you would..." > >It also takes a TYPE as an argument instead of an EXPRESSION. > >Michael You are 100% incorrect. Remember that the topic of discussion here is C++, not C! sizeof is certainly an operator. And it can take a type or an expression as an argument. See Stroustrup, pp. 257-258. It is, however, resolved at compile time. Presumably, it can't be overloaded for this very reason; it is syntactically an operator, but it is not treated the same as other operators. It may be overloadable in the future; C++ is still an evolving language, and recently -> was added to the list of overloadable operators. However, it would be much nicer if it used the dynamic rather than static information where appropriate, like class/structure element dereferencing!! Are you listening, Bjarne??? Unfortunately, for now, there seems to be no alternative but to keep track of the sizes yourself. Not very helpful, I know, but at least correct. :-) Bob Hearn hearn@claris.com
bph@buengc.BU.EDU (Blair P. Houghton) (03/31/89)
In article <8903281246.AA26233@wheat-chex.ai.mit.edu> hearn@claris.com writes: >In article <8903202240.AA24954@yahi> tiemann@lurch.stanford.edu writes: >>[...And I wrote...but someone lopped off the attribution...] >>>In article <123@riunite.ACA.MCC.COM> rfg@riunite.UUCP (Ron Guilmette) writes: >>>> ["why isn't sizeof declarable as an operator?"] >>> >>> Because it's a compiler directive, not a C-language operator. The >>> result of sizeof (something) is an integer _constant_. If you try to >>> take the size of something that has a varying size, you will get only >>> the size of that thing at compile time. >>> >>> If you're creating varying-sized data elements, you should keep track of >>> their sizes yourself. >> >> It also takes a TYPE as an argument instead of an EXPRESSION. > >You are 100% incorrect. Remember that the topic of discussion here is >C++, not C! sizeof is certainly an operator. And it can take a type >or an expression as an argument. See Stroustrup, pp. 257-258. It is, >however, resolved at compile time. Hurh? If it's resolved at compile time, how can it be an operator that returns the value of a varying quantity? Oh. I get it. I mean "it's not a FUNCTION, it's a compiler-directing operator..." Sorry. I zegged when I shoulda zogged. Not enuff wheat-chex for breakfast. [...discussion of non-overloadability of sizeof deleted...] >Unfortunately, for now, there seems to be no alternative but to keep track >of the sizes yourself. Not very helpful, I know, but at least correct. :-) --Blair "Deja vu."