Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c++ > #10465

Re: Virtual function call optimization

From Miles Bader <miles@gnu.org>
Newsgroups comp.lang.c++
Subject Re: Virtual function call optimization
Date 2011-09-25 22:58 +0900
Message-ID <87litczptk.fsf@catnip.gol.com> (permalink)
References <55363b39-cfd1-4ec2-be5a-fa180c64c557@f6g2000vbm.googlegroups.com>

Show all headers | View raw


cppquester <cppquester@googlemail.com> writes:
> class B { public:  virtual void foo() {} };
> class D: public B { public: void foo() {} }
> int main () { D* d = new D(); d->foo(); }
>
> Will foo() be called using late binding or like a regular function
> call?

It depends.  The compiler is allowed to do either.

Since the compiler can see the exact type of d in the above code, many
compilers are able to optimize the call to foo into a normal function
call, and even inline it, but other compilers will still call through
the vtable.

A quick check of some compilers installed here:

  g++ 4.4       calls through vtable
  g++ 4.5       calls through vtable
  g++ 4.6       direct call (but doesn't inline)
  g++ trunk     direct call (can inline)
  clang++ 2.9   direct call (can inline)
  clang++ trunk direct call (can inline)

-Miles

-- 
Ich bin ein Virus. Mach' mit und kopiere mich in Deine .signature.

Back to comp.lang.c++ | Previous | Next — Previous in thread | Find similar | Unroll thread


Thread

Virtual function call optimization cppquester <cppquester@googlemail.com> - 2011-09-25 05:42 -0700
  Re: Virtual function call optimization Leigh Johnston <leigh@i42.co.uk> - 2011-09-25 14:45 +0100
  Re: Virtual function call optimization Victor Bazarov <v.bazarov@comcast.invalid> - 2011-09-25 09:46 -0400
    Re: Virtual function call optimization cppquester <cppquester@googlemail.com> - 2011-09-25 20:24 -0700
      Re: Virtual function call optimization Victor Bazarov <v.bazarov@comcast.invalid> - 2011-09-26 08:51 -0400
        Re: Virtual function call optimization cppquester <cppquester@googlemail.com> - 2011-09-26 07:38 -0700
          Re: Virtual function call optimization "Kevin P. Fleming" <news@kpfleming.us> - 2011-09-26 09:59 -0500
            Re: Virtual function call optimization cppquester <cppquester@googlemail.com> - 2011-09-26 08:43 -0700
              Re: Virtual function call optimization Paavo Helde <myfirstname@osa.pri.ee> - 2011-09-26 11:51 -0500
                Re: Virtual function call optimization cppquester <cppquester@googlemail.com> - 2011-09-26 11:25 -0700
                Re: Virtual function call optimization Paavo Helde <myfirstname@osa.pri.ee> - 2011-09-26 14:48 -0500
                Re: Virtual function call optimization Richard Damon <news.x.richarddamon@xoxy.net> - 2011-09-26 19:42 -0400
                Re: Virtual function call optimization Miles Bader <miles@gnu.org> - 2011-09-27 13:47 +0900
  Re: Virtual function call optimization Miles Bader <miles@gnu.org> - 2011-09-25 22:58 +0900

csiph-web