Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8549041
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T13:38:05+00:00 2026-06-11T13:38:05+00:00

With code like below, can a compiler tell that a is in fact an

  • 0

With code like below, can a compiler tell that a is in fact an instance of B and optimize away the virtual table lookup?

#include <iostream>

class A
{
  public:
    virtual void f()
    {
        std::cout << "A::f()" << std::endl;
    }
};

class B : public A
{
  public:
    void f()
    {
        std::cout << "B::f()" << std::endl;
    }
};

int main()
{
    B b;
    A* a = &b;
    a->f();

    return 0;
}

Additional question after the answers of Jonthan Seng and reima: In case gcc is used, would it be necessary to use any flags to force it to optimize the vtable lookup?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T13:38:06+00:00Added an answer on June 11, 2026 at 1:38 pm

    Clang can easily make this optimization, and even inlines the function call. This can be seen from the generated assembly:

    Dump of assembler code for function main():
       0x0000000000400500 <+0>: push   %rbp
       0x0000000000400501 <+1>: mov    %rsp,%rbp
       0x0000000000400504 <+4>: mov    $0x40060c,%edi
       0x0000000000400509 <+9>: xor    %al,%al
       0x000000000040050b <+11>:  callq  0x4003f0 <printf@plt>
       0x0000000000400510 <+16>:  xor    %eax,%eax
       0x0000000000400512 <+18>:  pop    %rbp
       0x0000000000400513 <+19>:  retq   
    

    I took the liberty of replacing std::cout << … by equivalent calls to printf, as this greatly reduces the clutter in the disassembly.

    GCC 4.6 can also deduce that no vtable lookup is needed, but does not inline:

    Dump of assembler code for function main():
       0x0000000000400560 <+0>: sub    $0x18,%rsp
       0x0000000000400564 <+4>: mov    %rsp,%rdi
       0x0000000000400567 <+7>: movq   $0x4007c0,(%rsp)
       0x000000000040056f <+15>:  callq  0x400680 <B::f()>
       0x0000000000400574 <+20>:  xor    %eax,%eax
       0x0000000000400576 <+22>:  add    $0x18,%rsp
       0x000000000040057a <+26>:  retq   
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Why in code like below is the .NET compiler not able to establish that
I've code like that below, what I want to achieve is to generate PDF
I have a code like this below, which gives me a $link that equals
I have a code like this below in /root_project/main.cpp : #include theoraplayer/TheoraVideoClip.h unsigned int
The code below demonstrates a behavior of gcc 4.6.2 that I can't account for.
my code like below public class User { public int ID { get; set;
I have a standard code like below to validate xml against xsd, but it
I am using SQL Developer to debug code like below, the output result will
This has been bothering me lately- when I use some code like below to
My code looks like below: private void LayoutRoot_Loaded(object sender, System.Windows.RoutedEventArgs e) { // TODO:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.