C++ virtual function calls

fjh@cs.mu.OZ.AU
Sun, 12 Nov 1995 07:49:45 GMT

          From comp.compilers

Related articles
[4 earlier articles]
Re: C++ virtual function calls cliffc@ami.sps.mot.com (1995-10-25)
Re: C++ virtual function calls joe@sanskrit.ho.att.com (1995-10-30)
Re: C++ virtual function calls jplevyak@violet-femmes.cs.uiuc.edu (John B. Plevyak) (1995-11-09)
Re: C++ virtual function calls cliffc@ami.sps.mot.com (1995-11-05)
Re: C++ virtual function calls martelli@cadlab.it (1995-11-05)
Re: C++ virtual function calls bothner@cygnus.com (Per Bothner) (1995-11-06)
C++ virtual function calls fjh@cs.mu.OZ.AU (1995-11-12)
Re: C++ virtual function calls jplevyak@pink-panther.cs.uiuc.edu (1995-11-16)
| List of all articles for this month |
Newsgroups: comp.compilers
From: fjh@cs.mu.OZ.AU
Keywords: C++, optimize
Organization: Compilers Central
References: 95-11-039
Date: Sun, 12 Nov 1995 07:49:45 GMT

John B. Plevyak writes:


> David L Moore (dlmoore@ix.netcom.com) wrote:
> : > for (...) {
> : > foo->virtual_method(...);
> : > }
> : > Are compilers smart enough to lift the virtual lookup out of the loop?
> : > Is this commonly done?


I don't know of any C++ compilers that are smart enough to perform this
optimization. The three I tried (g++ 2.7, an old Cfront 2.x, and SGI's
Delta-C++, which is based on the EDG front-end version 2.19) don't.


> Because of the low level semantics of C++ code, this optimization
> is not possible in general. Nothing prevents the programmer from
> overwriting the object at the end of foo to be any (compatible) type.


That's not correct. Although you can write code which does this, such
code is prohibited by the draft ANSI/ISO C++ standard (see section 3.8).
The sample program you listed has undefined behaviour.
The C++ committee was careful to make sure this optimization was allowed.


--
Fergus Henderson WWW: http://www.cs.mu.oz.au/~fjh
fjh@cs.mu.oz.au PGP: finger fjh@128.250.37.3
--


Post a followup to this message

Return to the comp.compilers page.
Search the comp.compilers archives again.