Path: utzoo!utgpu!news-server.csri.toronto.edu!rutgers!usc!zaphod.mps.ohio-state.edu!rpi!turing.cs.rpi.edu!adamsf From: adamsf@turing.cs.rpi.edu (Frank Adams) Newsgroups: comp.lang.c++ Subject: Re: Efficient compilation of virtual functions Message-ID: <7&`%1J-@rpi.edu> Date: 31 Aug 90 22:09:16 GMT References: <1990Aug22.193347.18486@ux1.cso.uiuc.edu> <6428@wolfen.cc.uow.oz> Reply-To: adamsf@turing.cs.rpi.edu (Frank Adams) Organization: RPI CS Dept. Lines: 59 In article amanda@iesd.auc.dk (Per Abrahamsen) writes: >It would be nice to be able to declare a class or even a member >function as "leaf". Even if we disregard the possible optimizations, >it would add to the programmers ability to document the classes. I proposed exactly this about a year ago in this forum. The only response I got was a note from Stroustrap, saying in effect that he was unwilling to put (more) pure performance hacks in the language. (Actually, I proposed declaring a member function or even a class as "leaf". The former seems more natural to me.) An alternative approach might be to use a pragma: #pragma leaf foo // class foo is a leaf #pragma leaf foo::bar() // method bar in class foo is a leaf I'm not sure what the C standard says about pragmas (I remember some language like "cannot change the semantics of the program", but I don't know if that made the final draft.) In any event, this isn't C. It seems to me that a reasonable constraint of this sort on pragmas is that the pragma cannot be allowed to change an incorrect program into a correct program. Except for the fact that compiling and running a program does not find all errors, this is equivalent to saying that a program that works with the pragma will still work if it is removed/ignored. ----------- Another approach would be to allow "exact" or "non-virtual" pointers. Then foo exact * p1; foo * p2; would declare p1 a pointer specifically to a foo, while p2 would point to an instance of foo or one of its subclasses. Exact pointers can be converted freely to ordinary pointers to the class (and thence to superclasses), but the reverse conversion should require a type-cast. This puts a bit more of the burden on the programmer than the "leaf" declaration, since each pointer must be so annotated, not just the class. On the other hand, it is applicable in cases where the former is not; I might know that a particular pointer is to a foo (perhaps because I just created it), even if foo does have subclasses. Another use for an exact keyword might be for member declarations: one can declare a member function which is not inherited by subclasses. class foo { int i; public: virtual void zap() {i = -1;} exact void zap() {i = 0;} } Then foo::zap() would set i to -1, but the method for foo inherited by subclasses would set i to 0. This particular example is contrived, but there have been times when I have wanted to do such a thing. (Creating an abstract class above foo is an alternative way to accomplish the same end.)